summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/funcutils/FileUtils.java
blob: 04ac6b06fe33571039405d696e890fb875270351 (plain)
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
package bjc.utils.funcutils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.function.BiPredicate;

/**
 * Utilities for doing things with files.
 *
 * @author ben
 */
public class FileUtils {
	/*
	 * @NOTE
	 * 
	 * If it becomes necessary, write another overload for this with all the
	 * buttons and knobs from walkFileTree.
	 */
	/**
	 * Traverse a directory recursively. This is a depth-first traversal.
	 *
	 * @param root
	 *                The directory to start the traversal at.
	 *
	 * @param predicate
	 *                The predicate to determine whether or not to traverse
	 *                a directory.
	 *
	 * @param action
	 *                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.
	 *
	 */
	public static void traverseDirectory(final Path root, final BiPredicate<Path, BasicFileAttributes> predicate,
			final BiPredicate<Path, BasicFileAttributes> action) throws IOException {
		Files.walkFileTree(root, new FunctionalFileVisitor(predicate, action));
	}
}