Interface FileVisitor<T>
Usage Examples: Suppose we want to delete a file tree. In that case, each directory should be deleted after the entries in the directory are deleted.
Furthermore, suppose we want to copy a file tree to a target location. In that case, symbolic links should be followed and the target directory should be created before the entries in the directory are copied.
-
Method Summary
All MethodsInstance MethodsAbstract MethodsModifier and TypeMethodDescriptionInvoked for a directory after entries in the directory, and all of their descendants, have been visited.Invoked for a directory before entries in the directory are visited.Invoked for a file in a directory.Invoked for a file that could not be visited.
-
Method Details
-
preVisitDirectory
Invoked for a directory before entries in the directory are visited.Parameters: dir - a reference to the directory attrs - the directory's basic attributes Returns: the visit result Throws: IOException - if an I/O error occursIf this method returns CONTINUE, then entries in the directory are visited. If this method returns SKIP_SUBTREE or SKIP_SIBLINGS then entries in the directory (and any descendants) will not be visited.
-
visitFile
Invoked for a file in a directory.Parameters: file - a reference to the file attrs - the file's basic attributes Returns: the visit result Throws: IOException - if an I/O error occurs -
visitFileFailed
Invoked for a file that could not be visited. This method is invoked if the file's attributes could not be read, the file is a directory that could not be opened, and other reasons.Parameters: file - a reference to the file exc - the I/O exception that prevented the file from being visited Returns: the visit result Throws: IOException - if an I/O error occurs -
postVisitDirectory
Invoked for a directory after entries in the directory, and all of their descendants, have been visited. This method is also invoked when iteration of the directory completes prematurely (by a visitFile method returning SKIP_SIBLINGS, or an I/O error when iterating over the directory).Parameters: dir - a reference to the directory exc - null if the iteration of the directory completes without an error; otherwise the I/O exception that caused the iteration of the directory to complete prematurely Returns: the visit result Throws: IOException - if an I/O error occurs
-
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2026, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.