-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirMaker.java
More file actions
45 lines (40 loc) · 1.39 KB
/
Copy pathDirMaker.java
File metadata and controls
45 lines (40 loc) · 1.39 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package cn.edu.ustc.aaron.common;
import java.io.File;
import java.io.IOException;
public class DirMaker {
/**
* Enhancement of java.io.File#createNewFile()
* Create the given file. If the parent directory don't exists, we will create them all.
* @param file the file to be created
* @return true if the named file does not exist and was successfully created; false if the named file already exists
* @see java.io.File#createNewFile
* @throws IOException
*/
public static boolean createFile(File file) throws IOException {
if(! file.exists()) {
makeDir(file.getParentFile());
}
return file.createNewFile();
}
/**
* Enhancement of java.io.File#mkdir()
* Create the given directory . If the parent folders don't exists, we will create them all.
* @see java.io.File#mkdir()
* @param dir the directory to be created
*/
public static void makeDir(File dir) {
if(! dir.getParentFile().exists()) {
makeDir(dir.getParentFile());
}
dir.mkdir();
}
public static void main(String args[]) {
String filePath = "./b/c.txt";
File file = new File(filePath);
try {
boolean created = createFile(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}