更新时间:2023-11-29 19:07:40
您应该替换
dirFile.mkdir();
到
dirFile.mkdirs();
示例:
if (!dirFile.exists()) { // if file doesn't exist
System.out.println(dirFile.mkdir());
System.out.println(dirFile.mkdirs());
}
对于第一个将生成false [并且将不创建目录],对于第二个将生成true,您将创建/demos/file/tmp/test
will yield false for the first [and no dir will be created], and true for the second, and you will have created /demos/file/tmp/test
mkdirs()还在此文件表示的路径中创建父目录.
mkdirs() also creates parent directories in the path this File represents.
用于mkdirs()的javadocs:
javadocs for mkdirs():
创建以此抽象路径名命名的目录,包括任何 必要但不存在的父目录.请注意,如果这 操作失败,它可能已经成功创建了一些 必要的父目录.
Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.
mkdir()的Javadocs:
javadocs for mkdir():
创建以此抽象路径名命名的目录.
Creates the directory named by this abstract pathname.
希望可以帮到您!