0

what's the difference between

fis =new FileInputStream(path+File.separator+"src"+File.separator+"main"+File.separator+"java"+File.separator+"com"+File.separator+"resources"+File.separator+"config.properties");

fis =new FileInputStream(path+"/src/main/java/com/resources/config.properties");

fis =new FileInputStream(path+"\\src\\main\\java\\com\\resources\\config.properties");

My teacher told me to use File.separator as it's platform independent but on the internet I read '/'would work on Macs and Windows. I tried '/'on windows and it works. if '/' works on Macs and Windows why do we need to use File.separator?

Wizard
  • 7
  • 1
  • You probably don't "need" to for a modern application, but why would you hardcode file separators and hope they work on every platform when Java provides a clean, simple, platform-independent way to do it? This kind of thing can prevent very frustrating bugs in the future and it's good practice to make your code as safe and portable as possible if you intend for it to run on multiple platforms (which for Java, is always). – Layne Bernardo May 18 '21 at 23:58
  • 1
    This was answered thoroughly on a previous thread. See: https://stackoverflow.com/questions/2417485/difference-between-file-separator-and-slash-in-paths/2417515 – Atmas May 19 '21 at 00:48
  • Thank you very much! – Wizard May 19 '21 at 01:44
  • When you reading a *resource,* you normally do not read it as a file, because most Java applications are packaged as a .jar file, and the entries inside a single .jar file are not themselves files. Instead, such entries are read using [Class.getResourceAsStream](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/Class.html#getResourceAsStream(java.lang.String)), which takes a String argument which is **not a filename.** The argument is a relative URL, and in URLs, the file separator is always forward slash (`/`) on all platforms. – VGR May 19 '21 at 02:02

2 Answers2

6

Forward-slash works sometimes on Windows. The file system and the kernel treat '/' and '\' equivalently in path names. This has always been the case in the Windows NT line (2000, XP, Vista, 7, 8, 10) but was never true for DOS-based Windows.

Application code is not necessarily so accommodating. Code written by someone unaware of this convention may not understand '/'.

Then, too, there's the problem of command-line processing, where '/' may indicate a switch rather than a path.

So, 'Windows' itself understands '/' and '\' to be equivalent in pathnames, but not all code that runs in Windows knows that.

tl;dr - sometimes it works, sometimes it does not.

iggy
  • 1,328
  • 4
  • 3
1

Just use java.io.File or java.nio.Path for all file handling and you should not even need to reference File.separator in awkward String concatenations.

Both of File and Path classes auto-correct forward-slash to the platform File.separator so forward-slash always works for Java on Windows.

Examples to try which all generate the same OS path and avoid use of "+":

String path = "somedir";
Path a = Path.of(path, "src", "main", "java", "com", "resources", "config.properties");
Path b = Path.of(path, "src/main/java/com/resources/config.properties");
Path c = Path.of(path).resolve("src/main/java/com/resources/config.properties");
File d = new File(path, "src/main/java/com/resources/config.properties");

On Windows the correct separator is used => somedir\src\main\java\com\resources\config.properties. If you don't have JDK11+, replace Path.of by Paths.get (JDK7+)

DuncG
  • 12,137
  • 2
  • 21
  • 33