public interface FileVisitor<T>
Files.walkFileTree
methods to visit each file in
a file tree.
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.
Path start = ... Files.walkFileTree(start, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { if (e == null) { Files.delete(dir); return FileVisitResult.CONTINUE; } else { // directory iteration failed throw e; } } });
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.
final Path source = ... final Path target = ... Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { Path targetdir = target.resolve(source.relativize(dir)); try { Files.copy(dir, targetdir); } catch (FileAlreadyExistsException e) { if (!Files.isDirectory(targetdir)) throw e; } return CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.copy(file, target.resolve(source.relativize(file))); return CONTINUE; } });
Modifier and Type | Method and Description |
---|---|
FileVisitResult |
postVisitDirectory(T dir,
IOException exc)
Invoked for a directory after entries in the directory, and all of their
descendants, have been visited.
|
FileVisitResult |
preVisitDirectory(T dir,
BasicFileAttributes attrs)
Invoked for a directory before entries in the directory are visited.
|
FileVisitResult |
visitFile(T file,
BasicFileAttributes attrs)
Invoked for a file in a directory.
|
FileVisitResult |
visitFileFailed(T file,
IOException exc)
Invoked for a file that could not be visited.
|
FileVisitResult preVisitDirectory(T dir, BasicFileAttributes attrs) throws IOException
If 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.
dir
- a reference to the directoryattrs
- the directory's basic attributesIOException
- if an I/O error occursFileVisitResult visitFile(T file, BasicFileAttributes attrs) throws IOException
file
- a reference to the fileattrs
- the file's basic attributesIOException
- if an I/O error occursFileVisitResult visitFileFailed(T file, IOException exc) throws IOException
file
- a reference to the fileexc
- the I/O exception that prevented the file from being visitedIOException
- if an I/O error occursFileVisitResult postVisitDirectory(T dir, IOException exc) throws IOException
visitFile
method returning SKIP_SIBLINGS
,
or an I/O error when iterating over the directory).dir
- a reference to the directoryexc
- 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 prematurelyIOException
- if an I/O error occurs Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2020, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
Scripting on this page tracks web page traffic, but does not change the content in any way.