4

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 ?

TheByeByeMan
  • 223
  • 2
  • 3
  • 7
  • 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

1 Answers1

14

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.

fheub
  • 154
  • 5
Kilian Foth
  • 107,706
  • 45
  • 295
  • 310
  • 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