Search code examples
javafilecross-platform

Platform independent paths in Java


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?


Solution

  • 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.