java.io.File中mkdir和mkdirs的差别

mkdirs()可以成功建立多级文件夹, mkdir()只能成功建立一级的文件夹,多层目录就不能创建成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
private String path = "D:/folder/subFolder";
private String fileName = "temp.txt";

@Test
public void testMkdir() throws Exception {

String tempPath = path.concat(File.separator).concat(fileName);

File file = new File(tempPath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdir();
}
file.createNewFile();
}

@Test
public void testMkdirs() throws Exception {

String tempPath = path.concat(File.separator).concat(fileName);

File file = new File(tempPath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();

}

程序运行将抛出:java.io.IOException: 系统找不到指定的路径。因为mkdir()方法没有成功创建多层目录。

建议使用:mkdirs()方法