Java to create a file path, file error
Is there such a problem in general or for several reasons:
1. The format of the file path in question (note the escape character)
2. The method in question
Create a file path common There are two ways:
createNewFile, mkdirs, mkdirs
they are subject File method is mainly used to get some information about the file itself, such as directory files are located, file read and write permissions and file length and the like, does not involve a file read and write operations.
Differences
such as: the need to create a path to D: \ test \ a.txt file;
Note:
1.mkdirs and mkdir are not abnormal, the source code has been packaged well anomaly;
2.createNewFile create the file must consider the exception handling, otherwise, the program will get an error (error message is: Unhandled exception: java.io.IOException);
Then the code is as follows:
String path="D:\\test\\a.txt";//注意转义字符
File file = new File(Path);
if(!file.exists()) {
file.getParentFile().mkdirs();//创建父级目录
file.createNewFile();//创建文件
} `