Walking the Directory Hierarchy – Java I/O: Part II

Walking the Directory Hierarchy

The walk() method of the Files class creates a stream to walk or traverse the directory hierarchy rooted at a specific directory entry.

Click here to view code image

static Stream<Path> walk(Path start, FileVisitOption… options)
                         throws IOException
static Stream<Path> walk(Path start, int depth,
                         FileVisitOption… options)
                         throws IOException

Return a Stream that is lazily populated with Path objects by walking the directory hierarchy rooted at the entry specified by the start parameter. The start parameter can be a directory or a file.

The first method is equivalent to calling the second method with a depth of Integer.MAX_VALUE. The methods traverse the entries depth-first to a depth limit that is the minimum of the maximum depth of the directory hierarchy rooted at the start entry and any depth that is specified or implied.

These methods do not follow symbolic links, unless the constant FileVisitOption.FOLLOW_LINKS is specified.

Example 21.5 illustrates using the walk() method. The hierarchy of directory a (p. 1347) is traversed to illustrate different scenarios. The code at (1) creates the symbolic link a/b/c/dir_link to the directory a/b, if necessary.

Example 21.5 Traversing the Directory Hierarchy

Click here to view code image

import java.io.IOException;
import java.nio.file.*;
import java.util.stream.Stream;
public class WalkTheWalk {
  public static void main(String[] args) throws IOException {
    // Creating symbolic link.                                        // (1)
    try {
      Path targetPath = Path.of(“.”, “a”, “b”);
      Path symbLinkPath  = Path.of(“.”, “a”, “b”, “c”, “dir_link”);
      if (Files.notExists(symbLinkPath, LinkOption.NOFOLLOW_LINKS)) {
        Files.createSymbolicLink(symbLinkPath, targetPath.toAbsolutePath());
      }
    } catch (IOException ioe) {
      ioe.printStackTrace();
      return;
    }
    // Do the walk.                                                    // (2)
    Path start = Path.of(“.”, “a”);
    int MAX_DEPTH = 4;
    for (int depth = 0; depth <= MAX_DEPTH; ++depth)  {                // (3)
      try(Stream<Path> stream = Files.walk(start, depth,               // (4)
                                           FileVisitOption.FOLLOW_LINKS)) {
        System.out.println(“Depth limit: ” + depth);
        stream.forEach(System.out::println);
      } catch (IOException ioe) {
        ioe.printStackTrace();
      }
    }
  }
}

Output from the program (edited to fit on the page):

Click here to view code image

Depth: 0
./a
Depth: 1
./a
./a/x.txt
./a/b
Depth: 2
./a
./a/x.txt
./a/b
./a/b/c
./a/b/d
Depth: 3
./a
./a/x.txt
./a/b
./a/b/c
./a/b/c/z.txt
./a/b/c/dir_link
./a/b/d
./a/b/d/y.txt
Depth: 4
./a
./a/x.txt
./a/b
./a/b/c
./a/b/c/z.txt
Exception in thread “main” java.io.UncheckedIOException:
    java.nio.file.FileSystemLoopException: ./a/b/c/dir_link


Leave a Reply

Your email address will not be published. Required fields are marked *