~/ learn/ java-from-zero/ cards/ Directory traversal with Files.list and Files.walk
1 of 4

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.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/bf4a596f-f0da-4a21-bcba-f2e1f61f1721/flashcard utf-8 LF