in Windows path separator and Linux path separator is different, when the absolute path is used directly, cross-platform will be exposed “No such file or diretory” exception.
p>
File
File file1 = new File (“C:\ TMP \test.txt”);
File file1 = new File (“C:\ TMP \test.txt”)
File file2 = new File (“/ TMP /test.txt”);
File file2 = new File (“/ TMP /test.txt”);
if cross-platform is considered, it is best to say:
File myFile = new File(“C:” + file.separator + “TMP” + file.separator, “test.txt”);
File myFile = new File(“C:” + file.separator + “TMP” + file.separator, “test.txt”);
The
File class has several static fields that are similar to separator, which are system related and should be used as far as possible in programming.
separatorChar
public static final char separatorChar
is the default name separator associated with the system. This field is initialized to the first character that contains the system property file.separator value. On UNIX systems, the value of this field is ‘/’; On Microsoft Windows, it is ‘\’.
separator
public static final String separator
is the system-specific default name separator, which is represented as a string for convenience. This string contains only one character, separatorChar.
pathSeparatorChar
public static final char pathSeparatorChar
is the system-dependent path separator. This field is initialized as the first character that contains the system property path.separator value. This character is used to separate filenames in a given file sequence in the form of a path list. On UNIX systems, this field is ‘:’; On Microsoft Windows, it is ‘; ‘.
pathSeparator
public static final String pathSeparator
is the system-dependent path separator, which is represented as a string for convenience. This string contains only one character, pathSeparatorChar.
p>
p>
strong> span> p>
☞ warm prompt: span> to return to my blog index span> strong> span> p>
p>
p>
p>
p>
p>
Read More:
- The solution cannot be separated due to a special separator
- Split keyword in ABAP when the separator is at the beginning and end of the string
- Java – read all the files and folders in a certain directory and three methods to get the file name from the file path
- Java operation temporary file creation and deletion
- File.createNewFile report errors java.io.IOException , resolved
- Java class file operation and exception
- Java file rename failure
- Error:java.io.FileNotFoundException: Path is not a file [How to Solve]
- [solution] the resource file cannot be found in the jar package of Java
- Java 8 Stream – Read a file line by line
- Solving Chinese garbled code in Java compressed file
- java.lang.IllegalArgumentException : URI scheme is not “file” error resolution
- maven_ Unable to create servlet file under Java Web project
- T omcat:removeGeneratedFiles Failed to delete the generated java file solution
- Building Eureka_ Server error: application failed to start with classpath:file :/C:/ProgramFiles/Java/jdk
- Eclipse .java File Syntax error, parameterized types are only available if source level is 1.5 or
- [Solved] Mac ES Startup Error: Exception in thread “main“ java.nio.file.NotDirectoryException
- Java uses ZipFile to decompress the zip file and runs normally under Windows. Linux reports an error
- Error in header file when calling OpenGL to open obj file in vs2013: unable to open include file: “GL / glut. H”: no such file or directories
- java.lang.NoSuchMethodError: org.json.JSONObject.put(Java/lang/String; Java/util/Collection;) “209151;”