In Java, Files.find(start, maxDepth, matcher) walks a directory tree like walk, but only returns paths for which your matcher returns true. The matcher is a BiPredicate<Path, BasicFileAttributes>, so it receives each path and its attributes.
Because the attributes are read during the walk anyway, checks such as attrs.isRegularFile() or attrs.size() cost nothing extra. Results come in no particular order, and the stream must be closed.
A static method of the final utility class java.nio.file.Files, available since Java 8. Call it on the class name, as in Files.find(...), after import java.nio.file.Files;. You never create a Files object.
Match regular files whose name ends in .java, at any depth.
Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class FindJavaSources {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Files.createDirectories(dir.resolve("src"));
Files.createDirectories(dir.resolve("test"));
Files.write(dir.resolve("src/app.java"), new byte[0]);
Files.write(dir.resolve("src/notes.txt"), new byte[0]);
Files.write(dir.resolve("test/apptest.java"), new byte[0]);
Files.write(dir.resolve("readme.md"), new byte[0]);
List<String> sources;
try (Stream<Path> hits = Files.find(dir, Integer.MAX_VALUE,
(p, attrs) -> attrs.isRegularFile() && p.getFileName().toString().endsWith(".java"))) {
sources = hits.map(p -> dir.relativize(p).toString().replace('\\', '/'))
.sorted()
.collect(Collectors.toList());
}
System.out.println("Java sources: " + sources);
deleteTree(dir);
}
static void deleteTree(Path root) throws IOException {
List<Path> all;
try (Stream<Path> s = Files.walk(root)) {
all = s.sorted(Comparator.reverseOrder()).collect(Collectors.toList());
}
for (Path p : all) {
Files.delete(p);
}
}
}
Output
Run with java FindJavaSources. Directory order is unspecified, so the program sorts the names before printing; the output is the same on Windows, macOS, and Linux.
Java sources: [src/app.java, test/apptest.java]
How It Works
Four files, two matches. notes.txt and readme.md fail the name test; the two .java files pass.
Attributes for free.attrs.isRegularFile() uses data the walk already read, so it is cheaper than calling Files.isRegularFile.
Relative and sorted. Paths are made relative to the temp folder and sorted, since the search order is unspecified.
Example 2 — Find Files Larger Than 100 Bytes
Store the condition in a named BiPredicate and use attrs.size().
Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class FindBigFiles {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path archive = Files.createDirectory(dir.resolve("archive"));
Files.write(dir.resolve("small.bin"), new byte[50]);
Files.write(dir.resolve("big.bin"), new byte[200]);
Files.write(archive.resolve("huge.bin"), new byte[500]);
BiPredicate<Path, BasicFileAttributes> big = (p, attrs) -> attrs.isRegularFile() && attrs.size() > 100;
List<String> found;
try (Stream<Path> hits = Files.find(dir, Integer.MAX_VALUE, big)) {
found = hits.map(p -> dir.relativize(p).toString().replace('\\', '/'))
.sorted()
.collect(Collectors.toList());
}
System.out.println("Over 100 bytes: " + found);
deleteTree(dir);
}
static void deleteTree(Path root) throws IOException {
List<Path> all;
try (Stream<Path> s = Files.walk(root)) {
all = s.sorted(Comparator.reverseOrder()).collect(Collectors.toList());
}
for (Path p : all) {
Files.delete(p);
}
}
}
Output
Run with java FindBigFiles. Directory order is unspecified, so the program sorts the names before printing; the output is the same on Windows, macOS, and Linux.
Over 100 bytes: [archive/huge.bin, big.bin]
How It Works
Known sizes. The three files hold 50, 200, and 500 zero bytes.
A reusable condition. Declaring the BiPredicate separately makes its two parameter types explicit and lets you pass it to several searches.
Directories excluded. The archive folder is rejected by isRegularFile(), so only the two large files remain.
Example 3 — Find Folders Up to One or Two Levels Deep
Match directories with maxDepth 1 and then 2. The start folder matches too.
Run with java FindShallowFolders. Directory order is unspecified, so the program sorts the names before printing; the output is the same on Windows, macOS, and Linux.
maxDepth 1: [., a, b]
maxDepth 2: [., a, a/deep, b]
How It Works
Root counts. The start folder is a directory at depth 0, so it matches and is printed as a dot.
One level. With maxDepth 1, a and b are found but a/deep is not reached.
Two levels. Raising the limit to 2 adds a/deep, which sorts between a and b.
Frequently Asked Questions
Files.find(dir, Integer.MAX_VALUE, (p, a) -> a.isRegularFile() && p.toString().endsWith(".txt")), inside try-with-resources.
A function taking the Path and its BasicFileAttributes and returning true to include the path.
find gives you the attributes without another file system call, which is faster when you filter on size, type, or time.
Only if the matcher accepts it, for example a matcher that checks isDirectory().
In no particular order. Sort the stream for stable output.