forked from winterbe/java8-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtil.java
More file actions
92 lines (84 loc) · 3.42 KB
/
Copy pathFileUtil.java
File metadata and controls
92 lines (84 loc) · 3.42 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.utils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class FileUtil {
public static boolean deleteDirectory(String path) {
Path dir = Paths.get(path);
Stream<Path> fileStream = null;
try {
fileStream = Files.walk(dir);
List<File> files = fileStream.sorted(Comparator.reverseOrder()).map(Path::toFile).collect(Collectors.toList());
for (File file : files) {
System.out.println(file.getAbsolutePath() + " isDir " + file.isDirectory());
if (!file.delete()) {
System.out.println("删除失败 " + file.getAbsolutePath());
return false;
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("删除目录失败 " + path);
return false;
} finally {
if (Objects.nonNull(fileStream)) {
fileStream.close();
}
}
System.out.println("删除目录成功 " + path);
return true;
}
public static boolean copyDirectory(String sourceDir, String destDir) {
Path source = Paths.get(sourceDir);
Path dest = Paths.get(destDir);
if (!dest.toFile().exists()) {
if (!dest.toFile().mkdir()) {
System.out.println("无法创建目录 " + destDir);
System.out.println("复制目录失败 " + sourceDir + " -> " + destDir);
return false;
}
}
dest = dest.resolve(source.getParent().relativize(source));
System.out.println(dest);
Stream<Path> pathStream = null;
try {
pathStream = Files.walk(source);
List<Path> paths = pathStream.sorted().collect(Collectors.toList());
for (Path path : paths) {
System.out.println(path.toString() + " isDir " + path.toFile().isDirectory());
Path targetPath = dest.resolve(source.relativize(path));
System.out.println(path + " -> " + targetPath);
if (path.toFile().isDirectory()) {
if (!targetPath.toFile().exists()) {
Files.copy(path, dest.resolve(source.relativize(path)), StandardCopyOption.REPLACE_EXISTING);
}
} else {
Files.copy(path, dest.resolve(source.relativize(path)), StandardCopyOption.REPLACE_EXISTING);
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("复制目录失败 " + sourceDir + " -> " + destDir);
return false;
} finally {
if (Objects.nonNull(pathStream)) {
pathStream.close();
}
}
System.out.println("复制目录成功 " + sourceDir + " -> " + destDir);
return true;
}
public static void main(String[] args) {
//deleteDirectory("D:\\nginx_path\\a");
copyDirectory("D:\\nginx_path\\a", "D:\\nginx_path\\parentDir2");
}
}