简体   繁体   English

如何区分窗口/ * nix路径

[英]How to differentiate between window / *nix paths

I'm working on restoration application where the restoration paths are either windows paths or unix paths (I don't know which straight up) and then I need to map them to an appropriate path in the current OS. 我正在恢复应用程序上工作,其中恢复路径是Windows路径或Unix路径(我不知道哪个向上),然后需要将它们映射到当前OS中的适当路径。

Obviously Windows ==> Windows, *nix ==> *nix are straight forward. 显然Windows ==> Windows,* nix ==> * nix是直接的。 However when the original paths are from Windows moving them to *nix is problematic since *nix would interpret these paths as a file name (for example C:\\folderA\\fileB.txt is mapped to file in *nix and not as a path of /folderA/fileB.txt) 但是,当原始路径来自Windows时,将它们移动到* nix是有问题的,因为* nix会将这些路径解释为文件名(例如C:\\ folderA \\ fileB.txt映射到* nix中的文件,而不是路径/folderA/fileB.txt)

What I want to do is parse the paths in advance, determine if they are windows/*nix paths and then treat them accordingly. 我想做的是预先解析路径,确定它们是否为windows / * nix路径,然后相应地对其进行处理。

Any suggestions? 有什么建议么?

Jonathan 乔纳森

The java.io.File class works fine with slash separated paths on both platforms. java.io.File类在两个平台上都可以使用斜杠分隔的路径。

You can also parse the paths for the File.separator values, which is based on the platform the program is running on. 您还可以解析File.separator值的路径,该值基于程序在其上运行的平台。

Since the Windows APIs treat backslash \\ and slash / identically as path separators, and since sane people on Unix don't embed backslashes in file names, you could simply convert backslashes to slashes. 由于Windows API将反斜杠\\和斜杠/视为路径分隔符,并且由于Unix上的健全人不会在文件名中嵌入反斜杠,因此您可以将反斜杠转换为斜杠。 That would leave you with just drive letters ( C: etc) at the start of the path to deal with. 这将使您在处理路径的开头仅带有驱动器号( C:等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM