import java.io.IOException;
import java.nio.file.*;
import java.util.stream.Stream;
class Demo {
void example() throws IOException {
// Every .java file under src/
try (Stream<Path> stream = Files.walk(Path.of("src"))) {
stream
.filter(Files::isRegularFile)
.filter(p -> p.toString().endsWith(".java"))
.forEach(System.out::println);
}
// Count files
try (var s = Files.walk(Path.of("."))) {
long count = s.filter(Files::isRegularFile).count();
System.out.println("file count: " + count);
}
// Limit depth
try (var s = Files.walk(Path.of("."), 2)) {
s.forEach(System.out::println);
}
// Use Files.find when you want a predicate that gets BasicFileAttributes too
try (var s = Files.find(Path.of("."), 5,
(p, attrs) -> attrs.size() > 1_000_000)) {
s.forEach(System.out::println);
}
}
}
Create a free account and build your private vault. Share publicly whenever you want.