Platform independent paths in Java
Solution 1
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 2
Just use /
. I've been using it for 23.5 years. Never a problem.
Solution 3
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
You can use the static field File.separator to retrieve the platform specific separator character for file paths
Solution 5
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();
Related videos on Youtube
jakewins
Updated on July 05, 2022Comments
-
jakewins almost 2 years
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?
-
Jeroen Rosenberg over 13 yearspathSeparator will retrieve the platform specific separator between paths ( ';' for unix ':' for windows). I think separator is more appropriate in this case.
-
Thomas Lötzer over 13 yearspathSeparator is the separator for different entries in the PATH environment variable. For the question, File.separator would be the correct choice.
-
user207421 about 11 years@Downvoter It is now 16 years and I've still never had a problem. Your point?
-
user207421 about 11 years@ses that's what the answer is about too. I have no idea what your first sentence is supposed to mean.
-
John Alexander Betts over 10 yearsIf I am searching for a file in /opt/template using java, is this equivalent to c://opt/template in windows?
-
Mohnish almost 10 yearsWould work until some new OS doesn't come up with new exotic path separator! Maybe even then it would work, if JVM can handle "/" to that specific OS path separator conversion! So, generally "/" is a safe choice, but using explicit path separator is future fool proofing.
-
user207421 almost 10 years@Mohnish It would still work because Java would substitute, just like it does on Windows.
-
Neeme Praks over 9 years"You can use any path separator in Java, it will work on both Unix and Windows." -- wrong. If you use backslash () on Unix, it will not be recognized as a file path separator, it will be interpreted as a file name (Unix file names are allowed to contain backslashes).
-
aapierce over 8 yearsI tried using
pathSeparator
in my application, but on OS X, that's a colon (:
). If you toss that into a file path, OS X will interpret it as a forward slash character within the folder name. ex: Instead of creating a folder namedfoo
on the user's desktop, I accidentally created a folder literally namedDesktop/foo
in the user's home folder. Using a plain forward slash (/
) works exactly as expected, however. -
user207421 about 8 years@AechoLiu That's just another SO answer. No more authoritative than this in, which is at least evidence-based. A proper citation would be from the JLS or JVM Specification or the Javadoc.
-
AechoLiu about 8 years@EJP Sorry, I read the
Update
and saw the wordsNow, I'm sure I've seen it documented
in that SO answer. So I believe it should be true. I encounter this problem today and found answers in SO. And you and that SO answer help me to solve that problem. I just don't want any downvotes in this anser, beacause I know you are right. -
RossBille about 8 yearsThis answer requires a better explanation.
pathSeparator
andpathSeparatorChar
refer to the separator used in the system'sPATH
variable. The question is referring to file paths, not thePATH
variable. Thus,separator
andseparatorChar
should be used. -
Sipka almost 8 yearsYou can use
/
only in specific cases. If you need to compare different paths then you might need to convert the different separators to an unified one, in order to get the correct results. For example:Paths.get("a/b").equals(Paths.get("a\\b"))
yieldsfalse
result (same with compareTo method, doesn't return 0), meanwhile the following:Paths.get("a/b").equals(Paths.get("a/b"))
returnstrue
. Given this example, I strongly discourage the only use of/
separator, since it might unexpectedly break your code interoperability between platforms. -
Sipka almost 8 yearsOther Path class methods uses the platform separator, so here:
Paths.get("a/b").resolve("c")
on Windows, will result in `a/b\c' which will work with File opening, and other use-cases, but in equality testing, still could result in unexpected bugs. -
user207421 almost 8 years@Spika The point is that
/
is the 'unified one'. Not a 'specific case'. -
user207421 almost 8 years@ColinHebert The answer isn't right as long as it talks about path separators.
-
FonzTech over 3 yearsI love how the comment author edits this answer every year :')
-
james.garriss over 3 yearsDoesn't actually answer the question.
-
John almost 3 years@NotAJavaGuy suggests that using "/" might be a problem because then you couldn't use "split" to find the filename. I would say that using "split" is the problem here. The better approach would be
new File(filename).getName()