|
NIO2 API | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
public interface FileVisitor<T extends FileRef>
A visitor of files. An implementation of this interface is provided to the
walkFileTree utility method to visit each file
in a 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) {
try {
file.delete(false);
} catch (IOException exc) {
// failed to delete
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException e) {
if (e == null) {
try {
dir.delete(false);
} catch (IOException exc) {
// failed to delete
}
} else {
// directory iteration failed
}
return FileVisitResult.CONTINUE;
}
});
Furthermore, suppose we want to copy a file tree rooted at a source directory 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) {
try {
dir.copyTo(target.resolve(source.relativize(dir)));
} catch (FileAlreadyExistsException e) {
// ignore
} catch (IOException e) {
// copy failed, skip rest of directory and descendants
return SKIP_SUBTREE;
}
return CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
try {
file.copyTo(target.resolve(source.relativize(file)));
} catch (IOException e) {
// copy failed
}
return CONTINUE;
}
});
| Method Summary | |
|---|---|
FileVisitResult |
postVisitDirectory(T dir,
java.io.IOException exc)
Invoked for a directory after entries in the directory, and all of their descendants, have been visited. |
FileVisitResult |
preVisitDirectory(T dir)
Invoked for a directory before entries in the directory are visited. |
FileVisitResult |
preVisitDirectoryFailed(T dir,
java.io.IOException exc)
Invoked for a directory that could not be opened. |
FileVisitResult |
visitFile(T file,
BasicFileAttributes attrs)
Invoked for a file in a directory. |
FileVisitResult |
visitFileFailed(T file,
java.io.IOException exc)
Invoked for a file when its basic file attributes could not be read. |
| Method Detail |
|---|
FileVisitResult preVisitDirectory(T dir)
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 directory
FileVisitResult preVisitDirectoryFailed(T dir,
java.io.IOException exc)
dir - A reference to the directoryexc - The I/O exception thrown from the attempt to open the directory
FileVisitResult visitFile(T file,
BasicFileAttributes attrs)
file - A reference to the fileattrs - The file's basic attributes
FileVisitResult visitFileFailed(T file,
java.io.IOException exc)
file - A reference to the fileexc - The I/O exception thrown from the attempt to read the file
attributes
FileVisitResult postVisitDirectory(T dir,
java.io.IOException exc)
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 prematurely
|
NIO2 API | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
Copyright 2010 Sun Microsystems, Inc. All rights reserved. Use is subject to the terms of the GNU General Public License.