I've seen some programmers doing the following :

File file = new File("folder\\subfolder\\subsubfolder");

And I find it totally wrong because of compatibility issues with a different OS than Windows.

So is there one single reason why using double backslashes is preferable than forward slashs or File.separator ?

  • 1
    Note: these are not double backslashes. They are single backslashes. The first one in each pair is not part of the string, it's part of the Java string literal syntax (the escape character, to be precise). – Jörg W Mittag Apr 25 '16 at 11:33

No, there isn't. Slashes work everywhere, backslashes work only on Windows and are a pain to type and read. They are used only by people who mistakenly think they have to use them.

But of course, you should really be using the Paths API (documentation for JDK8, JDK9) and never write explicit path separators anyway.

  • 1
    Occasionally I'll see someone using an alternate file system that requires a specific slash type, so they don't want to use a separator that changes in different os's. ZipFileSystem comes to mind -- it is always "/", not os-dependent. Even in those cases people should use FileSystem.getSeparator() to get the appropriate separator for that file system. – Hank D Apr 25 '16 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.