jonatzin
jonatzin

Reputation: 972

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.

Obviously Windows ==> Windows, *nix ==> *nix are straight forward. 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)

What I want to do is parse the paths in advance, determine if they are windows/*nix paths and then treat them accordingly.

Any suggestions?

Jonathan

Upvotes: 0

Views: 163

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 753585

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. That would leave you with just drive letters (C: etc) at the start of the path to deal with.

Upvotes: 2

Dan D.
Dan D.

Reputation: 32391

The java.io.File class works fine with slash separated paths on both platforms.

You can also parse the paths for the File.separator values, which is based on the platform the program is running on.

Upvotes: 2

Related Questions