Platform independent paths in Java

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


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


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.


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.