Java Reference
In-Depth Information
METHOD
DESCRIPTION
This is called immediately before the entries in the directory dir are visited. The second argu-
ment is the directory's basic attributes. This method provides you with the means of con-
trolling what happens next. You should return one of the following constants defined by the
FileVisitResult enum that is in the java.nio.file package. CONTINUE : Entries in dir are
visited. SKIP_SIBLINGS or SKIP_SUBTREE : Entries in dir and any subdirectories will not be
visited.
preVisitDirectory(
Path dir,
BasicFileAttributes
attr)
This is called after entries in the dir directory and their descendants have been visited. This
method is also called when visiting entries in the directory is terminated by the visitFile()
method returning SKIP_SIBLINGS or an I/O error occurs. If an I/O error occurs while entries
are being visited, e references the exception object that was thrown. Otherwise e is null .
postVisitDirectory(
Path dir, IOExcep-
tion e)
This is called when the file file is visited. The attr parameter references the attributes for
the file.
visitFile( Path
file, BasicFileAt-
tributes attr)
This is called when the attributes for file cannot be read or the directory containing the file
cannot be opened. e references the exception object thrown as a result of an I/O error.
visitFileFailed(
Path file, IOExcep-
tion e)
There are two versions of the static walkFileTree() method. The simplest accepts two arguments:
• A Path object specifying the starting directory for the tree to be walked.
• A reference to a FileVisitor object that implements the methods mentioned earlier to do what
you want with the files and directories in the tree.
This version of the method does not follow links and visits all levels in the file tree. The other version of
walkFileTree() accepts four arguments:
• The starting directory path, as before.
• A set of java.nio.file.FileVisitOption enumeration constants, that contains either
FOLLOW_LINKS supplied in an EnumSet or an empty EnumSet . Specifying FOLLOW_LINKS causes
file links to be followed.
• A value of type int specifies the maximum depth to be explored in the file tree.
• The FileVisitor object that implements the methods that are called in the process of walking the
file tree.
The class java.util.EnumSet defines an object that can store a set of enumeration constants. You learn
more about the EnumSet class in Chapter 14 so just take it for granted for the moment. To specify an
EnumSet containing FOLLOW_LINKS you can use the expression EnumSet.of(FOLLOW_LINKS) . To define
an empty EnumSet of FileVisitOption constants you use the expression
EnumSet.noneOf(FileVisitOption.class) . The expressions assume that you have imported the con-
stants from the enumeration and you have an import statement for EnumSet .
The process for walking a file tree may seem a little hazy at this point, but an example should clarify it.
TRY IT OUT: Walking a File Tree
First I will define a class that implements the FileVisitor<Path> interface:
Search WWH ::




Custom Search