Java to create a file path, file error may be a key issue

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/HS2529077916/article/details/100545719

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;
Here Insert Picture DescriptionNote:
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();//创建文件
    } `


Guess you like

Origin blog.csdn.net/HS2529077916/article/details/100545719