0

we can use separator as "\" or "/" . And my question is is there any difference between them? For example;

 new File("c:\\temp.txt");

or

 new File("c:/temp.txt");   
BenMorel
  • 34,448
  • 50
  • 182
  • 322
cycklt
  • 5
  • 1
  • 4
  • AFAIK the first is used for Windows systems, the second for unix-based systems (Linux, Mac...). But the second is the "universal" separator, so it works on windows too. But wait for someone else to give you a more detailed explaination – BackSlash Jun 08 '14 at 21:10
  • If you want to make sure that it always fits the platform it is running on, you can use `File.separator`. – Felk Jun 08 '14 at 21:12
  • Yes the second one works on windows. And I said "both of them works so there must be a difference". Thanks for comment – cycklt Jun 08 '14 at 21:13

1 Answers1

3

There's a duplicate of this around somewhere, but if you use JDK methods, it doesn't matter which you use as they'll normalize them before they get to the OS. That cannot necessarily be said for other libraries. For max compatibility, use File.separator/File.separatorChar to get the OS-specific version. (\ on Windows, / on just about everything else.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875