SimpleFileVisitor Class

Package: java.nio.file

The SimpleFileVisitor class is a default implementation of the FileVisitor interface. This interface is used along with the walkFileTree method of the Files class to process all of the files in a given directory tree, including files in sub­directories. For more information, see FileVisitor Interface and Files Class.

Methods

Method

Description

FileVisitResult post VisitDirectory(T dir, IOException e)

Called once for every directory in the file tree. This method is called after all the files in the directory are visited.

FileVisitResult pre VisitDirectory(T dir)

Called once for every directory in the file tree. This method is called before any of the files in the directory are visited.

FileVisitResult visit File(T file, Basic FileAttributes attr)

Called once for every file in the file tree.

FileVisitResult visitFileFailed(T file, IOException e)

Called if the file couldn’t be accessed.

The following is an overview of what you must do to use the SimpleFileVisitor class to walk the contents of a directory tree:

1. Create a file visitor class that extends SimpleFileVisitor .

The SimpleFileVisitor class implements the four required methods of the FileVisitor class, providing default implementations for each method.

2. In your file visitor class, override one or more SimpleFileVisitor methods.

These methods are where you write the code that you want to execute for every file visited when the directory ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.