FileVisitor FileVisitResult FileVisitResult.CONTINUE FileVisitResult.SKIP_SIBLINGS FileVisitResult.SKIP_SUBTREE FileVisitResult.TERMINATE postVisitDirectory() preVisitDirectory() recursive folder recursive folder walks SimpleFileVisitor visitFile() visitFileFailed() Writing a file search application based on a criteria Writing an application that find files bigger than a pre-defined size

Recursive folder walks using NIO.2 API – andreINC

FileVisitor Interface

The FileVisitor interface permits us to recursively traverse file buildings – folders, sub-folders and information.

Each technique of this interface can return four attainable results (situations of the FileVisitResult enum):

This FileVisitor interface has four strategies:

  • visitFile():
    The tactic is invoked for a file. The tactic should return a FileVisitResult.CONTINUE outcome or a FileVisitResult.TERMINATE outcome. The tactic receive a reference to the file (a Path object) and to the BasicFileAttributes object associated with the Path.
  • preVisitDirectory():
    This technique is invoked for a listing before visiting its youngsters. The tactic returns FileVisitResult.CONTINUE if we would like it’s youngsters to be visited or FileVisitResult.SKIP_SUBTREE if we would like the process to cease. If we need to skip visiting the siblings of the directory we have to return FileVisitResult.SKIP_SIBLINGS .
  • postVisitDirectory():
    This technique is invoked after we go to all the youngsters of a listing (together with different folders and their descendants).
  • visitFileFailed():
    This technique is invoked if a file (or folder) can’t be accessed.

In follow it’s also attainable to make use of the SimpleFileVisitor class if we need to traverse solely the directories.

Once we’ve got created the “recursive-walking-mechanism” by implementing FileVisitor interface or by extending the SimpleFileVisitor class we will begin the recursive process by calling the walkFileTree() technique .


Example: Writing an software that search for information greater than a pre-defined measurement

On this example we’re going to implement a FileVisitor that walks a folder and logs to output all information which are greater than specific amount.

The first step is to put in writing the FileVisitor:

The primary technique:

And some sample output from my machine:


Writing a file search software based mostly on a standards

We will prolong the instance from above and create a extra common strategy.

The thought is to write down an summary implementation of the FileVisitor interface, that accommodates an abstract technique “standards(Path, BasicFileAttributes)“.

Later we will use anonymous courses to define a new conduct of our visitors specifying only the standards and avoiding to put in writing the boiler-plate-code necessary to implement a FileVisitor.

We’ll identify our FileVisitor implementation FileSearchByCriteriaVisitor:

This class may have two occasion variables referred to as outcomes and failedVisits:

The “criteria(Path, BasicFileAttributes)” talked about earlier than will probably be used like this:

Now everytime we implement a brand new FileSearchByCriteriaVisitor we should provide an implementation for the summary technique outlined before.

Instance the best way to use the FileSearchByCriteriaVisitor:

The results and the paths with errors can be found in “sizeVisitor.getResults()” and “sizeVisitor.getFailedVisits()”.