forked from redmouth/java2cpp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileUtils.java
More file actions
73 lines (63 loc) · 2.31 KB
/
Copy pathFileUtils.java
File metadata and controls
73 lines (63 loc) · 2.31 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package co.deepblue.java2cpp.util;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
/**
* Created by levin on 17-5-7.
*/
public class FileUtils {
public static BufferedWriter createWriter(Path fileDir, String fileName) throws Exception {
return createWriter(fileDir.toString(), fileName);
}
public static BufferedWriter createWriter(String fileDir, String fileName) throws Exception {
Path filePath = Paths.get(fileDir, fileName);
File file = filePath.toFile();
if (!file.exists())
file.createNewFile();
return new BufferedWriter(new FileWriter(file));
}
public static void copyDirectory(String srcDir, String dstDir) {
File dstFile = new File(dstDir);
dstFile.mkdirs();
File file = new File(srcDir);
File[] filelist = file.listFiles();
if (filelist != null) {
for (File f : filelist) {
if (f.isDirectory()) {
copyDirectory(PathUtils.concatenate(srcDir, f.getName()),
PathUtils.concatenate(dstDir, f.getName()));
} else {
if (f.getName().endsWith(".h") || f.getName().endsWith(".cpp") || f.getName().endsWith(".cxx")) {
try {
Files.copy(Paths.get(f.getAbsolutePath()), Paths.get(dstDir, f.getName()), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
public static void deleteDirectory(File file)
throws IOException {
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
if (files.length == 0) {
file.delete();
} else {
for (File f : files) {
deleteDirectory(f);
}
File[] fList = file.listFiles();
if (fList != null && fList.length == 0)
file.delete();
}
}
} else {
file.delete();
}
}
}