Java Reference ยท I/O and Files
Files.walk() and Files.list()
Reference for Files.list, Files.walk, Files.find and walkFileTree: list a directory, traverse a tree lazily, filter by attributes, and close the stream.
Files.list(dir) returns a lazy Stream<Path> of the entries directly inside a directory. Files.walk(dir) goes deeper: it visits the directory itself and everything below it, depth first. Files.find(dir, depth, matcher) is a walk with a filter on file attributes, and Files.walkFileTree is the callback-based version for full control (including deleting trees).
All the stream-returning methods hold an open directory handle, so you must close the stream, normally with try-with-resources. The order of entries is not specified: sort when you need stable output.
try (Stream<Path> entries = Files.walk(root)) { entries.filter(Files::isRegularFile).forEach(System.out::println); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
static Stream<Path> list(Path dir) throws IOException static Stream<Path> walk(Path start, FileVisitOption... options) throws IOException static Stream<Path> walk(Path start, int maxDepth, FileVisitOption... options) throws IOException static Stream<Path> find(Path start, int maxDepth, BiPredicate<Path, BasicFileAttributes> matcher, FileVisitOption... options) throws IOException static Path walkFileTree(Path start, FileVisitor<? super Path> visitor) throws IOException
| Name | Description |
|---|---|
dir / start |
The directory to read. |
maxDepth |
How many levels to descend. 0 is the start only, 1 its direct children, Integer.MAX_VALUE is unlimited (the default for walk). |
options |
FileVisitOption.FOLLOW_LINKS to follow symbolic links (cycles are detected). |
matcher |
For find: receives each path and its attributes, already read, which avoids extra system calls. |
visitor |
Callbacks preVisitDirectory, visitFile, visitFileFailed, postVisitDirectory. |
Returns: a lazy stream that must be closed (walkFileTree returns the start path).
Throws: NotDirectoryException if list is given a file, NoSuchFileException, AccessDeniedException. While the stream is consumed, I/O errors surface as UncheckedIOException.
Usage notes
walkincludes the start directory itself as its first element;listnever does.- Order is unspecified for all of them. Sort before printing or comparing.
- Close the stream. Leaking it keeps directory handles open; on Windows that can also block deleting the directory.
- Deleting a tree:
walksorted in reverse order (children before parents) works, as doeswalkFileTreewithpostVisitDirectory. Directories must be empty before they can be deleted. - Weakly consistent: files created or removed during the traversal may or may not appear.
- Symbolic links are not followed unless you ask for
FOLLOW_LINKS. listis not recursive; useDirectoryStream(Files.newDirectoryStream(dir, "*.java")) when you want a glob filter on one level.- Permissions: an unreadable subdirectory makes
walkfail withUncheckedIOException;walkFileTreelets you handle it invisitFileFailed.
Related
Path,Files.copy(),move(),delete(),Files.createTempFile(),File- Lessons: Files, Streams