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
|
package bjc.utils.funcutils;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.function.BiPredicate;
/**
* Utilities for doing things with files
*
* @author ben
*
*/
public class FileUtils {
private static final class FunctionalFileVisitor
extends SimpleFileVisitor<Path> {
private BiPredicate<Path, BasicFileAttributes> traversalPredicate;
private BiPredicate<Path, BasicFileAttributes> traversalAction;
public FunctionalFileVisitor(
BiPredicate<Path, BasicFileAttributes> traversalPredicate,
BiPredicate<Path, BasicFileAttributes> traversalAction) {
this.traversalPredicate = traversalPredicate;
this.traversalAction = traversalAction;
}
@Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) throws IOException {
if (traversalPredicate.test(dir, attrs)) {
return FileVisitResult.CONTINUE;
}
return FileVisitResult.SKIP_SUBTREE;
}
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
if (traversalAction.test(file, attrs)) {
return FileVisitResult.CONTINUE;
}
return FileVisitResult.TERMINATE;
}
}
/**
* Traverse a directory recursively. This is a depth-first traversal
*
*
* @param root
* The directory to start the traversal at
* @param traversalPredicate
* The predicate to determine whether or not to traverse a
* directory
* @param traversalAction
* The action to invoke upon each file in the directory.
* Returning true means to continue the traversal, returning
* false stops it
* @throws IOException
* if the walk throws an exception
*
* TODO If it becomes necessary, write another overload for
* this with all the buttons and knobs from walkFileTree
*/
public static void traverseDirectory(Path root,
BiPredicate<Path, BasicFileAttributes> traversalPredicate,
BiPredicate<Path, BasicFileAttributes> traversalAction)
throws IOException {
Files.walkFileTree(root, new FunctionalFileVisitor(
traversalPredicate, traversalAction));
}
}
|