Introduction to File. separator separators (Java ),
In windows, the path separator is different from the path Separator in Linux. When the absolute path is used directly, baolu across platforms will expose "no suchffile or directory ", this is often caused by incorrect path delimiters in the code.
For example, if you create a test.txt file under the tempdirectory, you should write the file in windows as follows:
File file1 = new File ("c: \ temp \ test.txt ");
In Linux, it is like this:
File file2 = new File ("/tmp/test.txt ")
If you want to consider cross-platform, it is best to write as follows:
File myFile = new File ("C:" + File. separator + "temp" + File. separator + "test.txt ")
The File class has several static fields similar to the separator, which are related to the system and should be used as much as possible during programming.
File. separatorChar
Public static final charString separator
Default name separator related to the system. This field is initialized to contain the first character of the system property file. separator value. On UNIX systems, the value of this field is '/'; on Microsoft Windows systems, it is '\'.
File. separator
Public static final String separatorcharseparatorChar
Default name separator related to the system. For convenience, it is represented as a string. This string contains only one character, namely separatorChar.
File. pathSeparatorChar
Public static final char pathSeparatorChar
System-related path separator. This field is initially the first character that contains the system attribute path. separator value. This character is used to separate file names in the file sequence given in the form of a path list. On UNIX systems, this field is ':'; on Microsoft Windows systems, it is ';'.
File. pathSeparator
Public static final String pathSeparator
System-related path separator, which is expressed as a string for convenience. This string contains only one character, namely pathSeparatorChar.