Type a Files.walk in try-with-resources filtering by extension
Type a Files.walk in try-with-resources filtering by extension
Answer
try (Stream<Path> s = Files.walk(Path.of("."))) { s.filter(p -> p.toString().endsWith(".txt")) .forEach(System.out::println); }
Files.walk is lazy — it reads the directory tree on demand. The try-with-resources ensures the underlying DirectoryStream is closed when done.