Introduction to path separators in Java

  • 2020-04-01 01:28:34
  • OfStack

Path separator:

Windows is the "\"

Unix | under Linux is "/"

Considering the portability of the program, it is recommended to use "/" when creating files, because it is also ok to use "/" under Windows platform when creating files with Java after testing. Java seems to have been processed in the background.

Another usage:

Replace them with file.separator, which never presents portability problems.

Here are some examples to deepen your understanding:

File f = new File("d://book//1");     Square root

File f = new File("d:/book/1"); Square root

File f = new File("d:\\book\\1"); Square root

File f = new File("d:\book\1");   Χ  

Note: \ is an escape character, so \\ corresponds to \.

 

Summary: "/" or" file.separator "is recommended.


Related articles: