Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Walking trees with globs

โญโญโญ advancedโณ 15 min read๐Ÿ“ Lesson 158 of 180

Lazy streams over directory trees, glob matchers, and descriptor hygiene.

Walking a tree is a pipeline, not a loop with recursion homework:

try (Stream<Path> paths = Files.walk(root, 3)) {
    List<Path> logs = paths
        .filter(p -> p.toString().endsWith(".log"))
        .collect(toList());
}

Files.walk lazily streams the tree (depth-bounded); Files.find folds a predicate in; Files.list covers a single directory. Glob matching is built in โ€” FileSystems.getDefault().getPathMatcher("glob:**/*.{log,tmp}") โ€” and must be applied against both the path and its filename form depending on the pattern's shape (dir/** matches path-syntax; *.log matches filenames). The stream is LAZY and backed by the directory iterator: closing it (try-with-resources) releases the handle โ€” leaking it leaks file descriptors.