javaplatform-independent

File path issue on windows vs Unix in java


In my program, I am reading a resource file for a unit test. I use file path as:

\\\path\\\to\\\file

On my machine(Windows) this runs fine. But on server(Unix), this fails, and I have to change it to: /path/to/file

But Java is supposed to be platform independent. So isn't this behaviour unexpected?


Solution

  • Use FileSystem.getSeparator() or System.getProperty("file.separator") instead of using slashes.

    EDIT: You can get an instance of FileSystem via FileSystems.getDefault (JDK 1.7+)