Recursive folder walks using NIO.2 API

FileVisitor Interface

The FileVisitor interface allows us to recursively traverse file structures – folders, sub-folders and files.

Every method of this interface can return 4 possible results (instances of the FileVisitResult enum):

This FileVisitor interface has 4 methods:

  • visitFile():
    The method is invoked for a file. The method should return a FileVisitResult.CONTINUE result or a FileVisitResult.TERMINATE result. The method receive a reference to the file (a Path object) and to the BasicFileAttributes object associated with the Path.
  • preVisitDirectory():
    This method is invoked for a directory before visiting its children. The method returns FileVisitResult.CONTINUE if we want it’s children to be visited or FileVisitResult.SKIP_SUBTREE if we want the process to stop. If we want to skip visiting the siblings of the directory we need to return FileVisitResult.SKIP_SIBLINGS .
  • postVisitDirectory():
    This method is invoked after we visit all the children of a directory (including other folders and their descendants).
  • visitFileFailed():
    This method is invoked if a file (or folder) cannot be accessed.

In practice it is also possible to use the SimpleFileVisitor class if we want to traverse only the directories.

Once we have created the “recursive-walking-mechanism” by implementing FileVisitor interface or by extending the SimpleFileVisitor class we can start the recursive process by calling the walkFileTree() method .


Example: Writing an application that search for files bigger than a pre-defined size

In this example we are going to implement a FileVisitor that walks a folder and logs to output all files that are bigger than certain amount.

The first step is to write the FileVisitor:

/**
 * This FileVisitor searches for files bigger than 'size' .
 * 
 * If a file matching our criteria is found, we log the results in the stdout .
 * 
 * @author AndreICiobanu
 * 
 */
class FileSizeVisitor implements FileVisitor<Path> {

  private Long size;

  public FileSizeVisitor(Long size) {
    this.size = size;
  }

  /**
   * This is triggered before visiting a directory.
   */
  @Override
  public FileVisitResult preVisitDirectory(Path path,
      BasicFileAttributes attrs) throws IOException {
    return FileVisitResult.CONTINUE;
  }

  /**
   * This is triggered when we visit a file.
   */
  @Override
  public FileVisitResult visitFile(Path path, BasicFileAttributes attrs)
      throws IOException {

    long fileSize = attrs.size() / 1024;

    if (fileSize >= this.size) {
      System.out.println("File bigger than " + this.size + "KB  found: "
          + path);
    }

    return FileVisitResult.CONTINUE;
  }

  /**
   * This is triggered if we cannot visit a Path We log the fact we cannot
   * visit a specified path .
   */
  @Override
  public FileVisitResult visitFileFailed(Path path, IOException exc)
      throws IOException {
    // We print the error
    System.err.println("ERROR: Cannot visit path: " + path);
    // We continue the folder walk
    return FileVisitResult.CONTINUE;
  }

  /**
   * This is triggered after we finish visiting a specified folder.
   */
  @Override
  public FileVisitResult postVisitDirectory(Path path, IOException exc)
      throws IOException {
    // We continue the folder walk
    return FileVisitResult.CONTINUE;
  }

}

The main method:

Path homeFolder = Paths.get("C:\\");
FileVisitor fileVisitor = new FileSizeVisitor(new Long(5000));
try {
  Files.walkFileTree(homeFolder, fileVisitor);
} catch (IOException e) {
  e.printStackTrace();
}

And some sample output from my machine:

...
File bigger than 5000  found: C:\Windows\System32\IME\imekr8\applets\mshwkorrIME.dll
File bigger than 5000  found: C:\Windows\System32\IME\IMETC10\applets\MSHWCHTRIME.dll
File bigger than 5000  found: C:\Windows\System32\korwbrkr.lex
ERROR: Cannot visit path: C:\Windows\System32\LogFiles\WMI\RtBackup
...


Writing a file search application based on a criteria

We can extend the example from above and create a more general approach.

The idea is to write an abstract implementation of the FileVisitor interface, that contains an abstract method “criteria(Path, BasicFileAttributes)“.

Later we can use anonymous classes to define a new behavior of our visitors specifying only the criteria and avoiding to write the boiler-plate-code necessary to implement a FileVisitor.

We will name our FileVisitor implementation FileSearchByCriteriaVisitor:

abstract class FileSearchByCriteriaVisitor implements FileVisitor<Path>

This class will have two instance variables called results and failedVisits:

private List<Path> results = new LinkedList<Path>();
private List<Path> failedVisits = new LinkedList<Path>();
public List<Path> getResults() {
  return this.results;
}
public List<Path> getFailedVisits() {
  return this.failedVisits;
}

The “criteria(Path, BasicFileAttributes)” mentioned before will be used like this:

// This method will be later implemented
protected abstract Boolean criteria(Path path, BasicFileAttributes attrs);
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
    throws IOException {
  // Everytime we visit a file we check if that particular file matches a
  // criteria .
  if (criteria(file, attrs)) {
    // If the file matches the criteria we add it as a result
    results.add(file);
  }
  return FileVisitResult.CONTINUE;
}

Now everytime we implement a new FileSearchByCriteriaVisitor we must supply an implementation for the abstract method defined before.

Example how to use the FileSearchByCriteriaVisitor:

// Search all the files bigger than size
final long size = 5000; // KB
// Defining a new visitor criteria
FileSearchByCriteriaVisitor sizeVisitor = new FileSearchByCriteriaVisitor() {
  @Override
  protected Boolean criteria(Path path, BasicFileAttributes attrs) {
    if (attrs.size() / 1024 >= size) {
      return true;
    }
    return false;
  }
};
// Walk don't run
Files.walkFileTree(Paths.get("C:\\"), sizeVisitor);
// Evaluate results
evaluate(sizeVisitor.getResults());

The results and the paths with errors are available in “sizeVisitor.getResults()” and “sizeVisitor.getFailedVisits()”.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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

Are we human, or are we dancer *