-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathFileUtils.java
More file actions
61 lines (58 loc) · 1.77 KB
/
FileUtils.java
File metadata and controls
61 lines (58 loc) · 1.77 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
package com.tedi.park.test;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.function.BiFunction;
/**
*
* @author KevinBlandy
*
*/
public class FileUtils {
/**
* 重命名指定文件夹下的所有文件,或者文件夹
* @param source
* @param func
* @throws IOException
*/
public static void renameDir(String sourceDir,BiFunction<String,Boolean,String> func) throws IOException {
File file = new File(sourceDir);
File[] subFiles = file.listFiles();
if(subFiles != null && subFiles.length > 0) {
for(File subFile : subFiles) {
boolean isDirectory = subFile.isDirectory();
if(isDirectory) {
renameDir(subFile.getAbsolutePath(),func);
}
subFile.renameTo(new File(subFile.getParentFile(), func.apply(subFile.getName(), isDirectory)));
}
}
}
/**
* copy整个文件夹
* @param source
* @param target
* @throws IOException
*/
public static void copyDir(String sourceDir, String targetDir) throws IOException {
Path sourcePath = Paths.get(sourceDir);
Path targetPath = Paths.get(targetDir);
Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
Files.copy(dir, targetPath.resolve(sourcePath.relativize(dir)));
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.copy(file, targetPath.resolve(sourcePath.relativize(file)));
return FileVisitResult.CONTINUE;
}
});
}
}