Java - Visit File Tree

Introduction

You can use FileVisitor to recursively process all files and directories in a file tree.

SimpleFileVisitor class is a basic implementation of the FileVisitor interface.

Methods of the FileVisitor Interface

Method
Description
FileVisitResult preVisitDirectory(T dir,
BasicFileAttributes attrs)
called once before visiting entries in a
directory.
FileVisitResult postVisitDirectory(T dir,
IOException exc)





called after entries in a directory and all
of their descendants have been visited.
If there was any exception thrown during the iteration of
a directory, the exception object is passed to this method
as the second argument. If the second argument to
this method is null, there was no exception during the
directory iteration.
FileVisitResult visitFile(T file,
BasicFileAttributes attrs)
called when a file in a directory is visited.

FileVisitResult visitFileFailed(T file,
IOException exc)
called when a file or directory could not
be visited for any reason.

Enum Constants of FileVisitResult and Their Descriptions

Enum Constant
Description
CONTINUE
Continues processing
SKIP_SIBLINGS


Continues processing without visiting the siblings of the file or directory. If it is returned from
the preVisitDirectory() method, the entries in the current directory is also skipped and the
postVisitDirectory() method is not called on that directory.
SKIP_SUBTREE

Continues processing without visiting entries in the directory. It is meaningful only when
returned from the preVisitDirectory() method. Otherwise, its effect is the same as CONTINUE.
TERMINATE
Terminates the file visiting process.

You do not need to write logic in all four methods.

The following code implement a file visitor to print the names of all files and subdirectories of a directory.

Demo

import static java.nio.file.FileVisitResult.CONTINUE;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class Main {
  public static void main(String[] args) {
    // Get the Path object for the default directory
    Path startDir = Paths.get("");

    // Get a file visitor object
    FileVisitor<Path> visitor = getFileVisitor();

    try {// www . j  ava 2s  .c o m
      // Traverse the contents of the startDir
      Files.walkFileTree(startDir, visitor);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static FileVisitor<Path> getFileVisitor() {
    // Declare a local class DirVisitor that
    // inherits from the SimpleFileVisitor<Path> class
    class DirVisitor<Path> extends SimpleFileVisitor<Path> {
      @Override
      public FileVisitResult preVisitDirectory(Path dir,
          BasicFileAttributes attrs) {
        System.out.format("%s [Directory]%n", dir);
        return CONTINUE;
      }

      @Override
      public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {

        System.out.format("%s [File, Size: %s bytes]%n", file, attrs.size());
        return CONTINUE;
      }
    }

    // Create an object of the DirVisitor
    FileVisitor<Path> visitor = new DirVisitor<>();

    return visitor;
  }
}

Result