summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcutils/FileUtils.java
blob: 6fc09ffc3d001c3caefac64394584542189f1bd5 (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
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 {
	/**
	 * 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
	 * 
	 *             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> predicate,
			BiPredicate<Path, BasicFileAttributes> action)
			throws IOException {
		Files.walkFileTree(root, new FunctionalFileVisitor(predicate, action));
	}
}