Platform independent paths in Java

JavaFileCross Platform

Java Problem Overview


I know the relative path of a file and want to handle it as a File object on both Linux and Windows.

What is the best way to specify platform-independent paths in Java?

Java Solutions


Solution 1 - Java

Just use /. I've been using it for 23.5 years. Never a problem.

Solution 2 - Java

The File class contains the following public members that you can use for platform specific file paths:

>static String pathSeparator :
The system-dependent path-separator character, represented as a string for convenience.
static char pathSeparatorChar :
The system-dependent path-separator character.
static String separator :
The system-dependent default name-separator character, represented as a string for convenience. static char separatorChar :
The system-dependent default name-separator character.

Solution 3 - Java

You can use any path separator in Java, it will work on both Unix and Windows. If you still want to use the system path separator there is the File.separator property which will give you the right one depending on the current system.

For the root, you can use listRoots() which gives you an array of root, there will be only one element on Unix systems, and as many as you have drives on Windows.

Solution 4 - Java

You can use the static field File.separator to retrieve the platform specific separator character for file paths

Solution 5 - Java

Java is pretty smart about paths in File objects. I just use something like "../foo/bar" and it works in those two platforms plus MacOSX.

Solution 6 - Java

java 7 also supports the use of Paths here

> The Path is obtained by invoking the getPath method of the default FileSystem.

You then may get a file from it by calling:

File fileSystemObtainedFile = Paths.get("C:\\foo\\bar.txt").toFile();

Solution 7 - Java

Personally, I like to use the Path class from Eclipse for handling paths in general, which you can just use standalone with few modifications as it's quite isolated.

http://grepcode.com/file/repository.grepcode.com/java/eclipse.org/3.5/org.eclipse.equinox/common/3.5.0/org/eclipse/core/runtime/Path.java/?v=source

Solution 8 - Java

Only 10 years too late.... The "It doesn't matter just use '/'" is only half true (ok, three quarters). You have to think about where your data is coming from.

If you get a path in Java, it will use '/' If you create a path in Java it will understand '/'

But what about paths that are input to your program?

Suppose I have a series of scripts that create a config (Properties) file. Suppose one of the config variables is INTERESTING_FILE, and we generate this to be a filename including path. Now suppose I want to extract the actual Filename from that, so I say

String[] filename = INTERESTING_FILE.split("/");

This will misbehave on Windows Systems, however

String[] filename = INTERESTING_FILE.split(pathSeparator);

will work (as will washing it through the Paths class).

I guess the point is, I wouldn't assume '/' is going to work in every case.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjakewinsView Question on Stackoverflow
Solution 1 - Javauser207421View Answer on Stackoverflow
Solution 2 - JavajjnguyView Answer on Stackoverflow
Solution 3 - JavaColin HebertView Answer on Stackoverflow
Solution 4 - JavaJeroen RosenbergView Answer on Stackoverflow
Solution 5 - JavaSteven OuradaView Answer on Stackoverflow
Solution 6 - JavaccDictView Answer on Stackoverflow
Solution 7 - JavaChris DennettView Answer on Stackoverflow
Solution 8 - JavaNotAJavaGuyView Answer on Stackoverflow