5

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 Answers1

15

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, JDK21) and never write explicit path separators anyway.

Kilian Foth
  • 110,899