FileVisitor
Stay organized with collections
Save and categorize content based on your preferences.
Known Indirect Subclasses
SimpleFileVisitor<T> |
A simple visitor of files with default behavior to visit all files and to
re-throw I/O errors. |
|
A visitor of files. An implementation of this interface is provided to the
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;
}
});
Public Methods
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 |
Invoked for a directory before entries in the directory are visited.
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.
Parameters
dir |
a reference to the directory |
attrs |
the directory's basic attributes |
Invoked for a file in a directory.
Parameters
file |
a reference to the file |
attrs |
the file's basic attributes |
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 |
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eFileVisitor\u003c/code\u003e interface provides a mechanism to visit each file in a file tree, commonly used with \u003ccode\u003eFiles.walkFileTree\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eIt defines methods to handle events for directories before (\u003ccode\u003epreVisitDirectory\u003c/code\u003e) and after (\u003ccode\u003epostVisitDirectory\u003c/code\u003e) their entries are visited, as well as for individual files (\u003ccode\u003evisitFile\u003c/code\u003e).\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003evisitFileFailed\u003c/code\u003e method is invoked when a file cannot be visited due to errors like inaccessible attributes or unopenable directories.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eSimpleFileVisitor\u003c/code\u003e provides a basic implementation with default behaviors for these methods, often used as a starting point for custom visitors.\u003c/p\u003e\n"],["\u003cp\u003eUsing \u003ccode\u003eFileVisitor\u003c/code\u003e allows operations like deleting or copying entire file trees with fine-grained control over how each file and directory is handled.\u003c/p\u003e\n"]]],[],null,["# FileVisitor\n\npublic interface **FileVisitor** \n\n|---|---|---|\n| Known Indirect Subclasses [SimpleFileVisitor](../../../../reference/java/nio/file/SimpleFileVisitor.html)\\\u003cT\\\u003e |--------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------| | [SimpleFileVisitor](../../../../reference/java/nio/file/SimpleFileVisitor.html)\\\u003cT\\\u003e | A simple visitor of files with default behavior to visit all files and to re-throw I/O errors. | |||\n\nA visitor of files. An implementation of this interface is provided to the\n[Files.walkFileTree](../../../../reference/java/nio/file/Files.html#walkFileTree(java.nio.file.Path,%20java.nio.file.FileVisitor\u003c?%20super%20java.nio.file.Path\u003e)) methods to visit each file in\na file tree.\n\n**Usage Examples:**\nSuppose we want to delete a file tree. In that case, each directory should\nbe deleted after the entries in the directory are deleted. \n\n```\n Path start = ...\n Files.walkFileTree(start, new SimpleFileVisitor\u003cPath\u003e() {\n @Override\n public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)\n throws IOException\n {\n Files.delete(file);\n return FileVisitResult.CONTINUE;\n }\n @Override\n public FileVisitResult postVisitDirectory(Path dir, IOException e)\n throws IOException\n {\n if (e == null) {\n Files.delete(dir);\n return FileVisitResult.CONTINUE;\n } else {\n // directory iteration failed\n throw e;\n }\n }\n });\n \n```\n\nFurthermore, suppose we want to copy a file tree to a target location.\nIn that case, symbolic links should be followed and the target directory\nshould be created before the entries in the directory are copied. \n\n```\n final Path source = ...\n final Path target = ...\n\n Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,\n new SimpleFileVisitor\u003cPath\u003e() {\n @Override\n public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)\n throws IOException\n {\n Path targetdir = target.resolve(source.relativize(dir));\n try {\n Files.copy(dir, targetdir);\n } catch (FileAlreadyExistsException e) {\n if (!Files.isDirectory(targetdir))\n throw e;\n }\n return CONTINUE;\n }\n @Override\n public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)\n throws IOException\n {\n Files.copy(file, target.resolve(source.relativize(file)));\n return CONTINUE;\n }\n });\n \n```\n\n\u003cbr /\u003e\n\n### Public Method Summary\n\n|--------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract [FileVisitResult](../../../../reference/java/nio/file/FileVisitResult.html) | [postVisitDirectory](../../../../reference/java/nio/file/FileVisitor.html#postVisitDirectory(T,%20java.io.IOException))(T dir, [IOException](../../../../reference/java/io/IOException.html) exc) Invoked for a directory after entries in the directory, and all of their descendants, have been visited. |\n| abstract [FileVisitResult](../../../../reference/java/nio/file/FileVisitResult.html) | [preVisitDirectory](../../../../reference/java/nio/file/FileVisitor.html#preVisitDirectory(T,%20java.nio.file.attribute.BasicFileAttributes))(T dir, [BasicFileAttributes](../../../../reference/java/nio/file/attribute/BasicFileAttributes.html) attrs) Invoked for a directory before entries in the directory are visited. |\n| abstract [FileVisitResult](../../../../reference/java/nio/file/FileVisitResult.html) | [visitFile](../../../../reference/java/nio/file/FileVisitor.html#visitFile(T,%20java.nio.file.attribute.BasicFileAttributes))(T file, [BasicFileAttributes](../../../../reference/java/nio/file/attribute/BasicFileAttributes.html) attrs) Invoked for a file in a directory. |\n| abstract [FileVisitResult](../../../../reference/java/nio/file/FileVisitResult.html) | [visitFileFailed](../../../../reference/java/nio/file/FileVisitor.html#visitFileFailed(T,%20java.io.IOException))(T file, [IOException](../../../../reference/java/io/IOException.html) exc) Invoked for a file that could not be visited. |\n\nPublic Methods\n--------------\n\n#### public abstract [FileVisitResult](../../../../reference/java/nio/file/FileVisitResult.html)\n**postVisitDirectory**\n(T dir, [IOException](../../../../reference/java/io/IOException.html) exc)\n\nInvoked for a directory after entries in the directory, and all of their\ndescendants, have been visited. This method is also invoked when iteration\nof the directory completes prematurely (by a [visitFile](../../../../reference/java/nio/file/FileVisitor.html#visitFile(T,%20java.nio.file.attribute.BasicFileAttributes))\nmethod returning [SKIP_SIBLINGS](../../../../reference/java/nio/file/FileVisitResult.html#SKIP_SIBLINGS),\nor an I/O error when iterating over the directory). \n\n##### Parameters\n\n| dir | a reference to the directory |\n| 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 |\n|-----|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n\n##### Returns\n\n- the visit result \n\n##### Throws\n\n| [IOException](../../../../reference/java/io/IOException.html) | if an I/O error occurs |\n|---------------------------------------------------------------|------------------------|\n\n#### public abstract [FileVisitResult](../../../../reference/java/nio/file/FileVisitResult.html)\n**preVisitDirectory**\n(T dir, [BasicFileAttributes](../../../../reference/java/nio/file/attribute/BasicFileAttributes.html) attrs)\n\nInvoked for a directory before entries in the directory are visited.\n\nIf this method returns [CONTINUE](../../../../reference/java/nio/file/FileVisitResult.html#CONTINUE),\nthen entries in the directory are visited. If this method returns [SKIP_SUBTREE](../../../../reference/java/nio/file/FileVisitResult.html#SKIP_SUBTREE) or [SKIP_SIBLINGS](../../../../reference/java/nio/file/FileVisitResult.html#SKIP_SIBLINGS) then entries in the\ndirectory (and any descendants) will not be visited. \n\n##### Parameters\n\n| dir | a reference to the directory |\n| attrs | the directory's basic attributes |\n|-------|----------------------------------|\n\n##### Returns\n\n- the visit result \n\n##### Throws\n\n| [IOException](../../../../reference/java/io/IOException.html) | if an I/O error occurs |\n|---------------------------------------------------------------|------------------------|\n\n#### public abstract [FileVisitResult](../../../../reference/java/nio/file/FileVisitResult.html)\n**visitFile**\n(T file, [BasicFileAttributes](../../../../reference/java/nio/file/attribute/BasicFileAttributes.html) attrs)\n\nInvoked for a file in a directory. \n\n##### Parameters\n\n| file | a reference to the file |\n| attrs | the file's basic attributes |\n|-------|-----------------------------|\n\n##### Returns\n\n- the visit result \n\n##### Throws\n\n| [IOException](../../../../reference/java/io/IOException.html) | if an I/O error occurs |\n|---------------------------------------------------------------|------------------------|\n\n#### public abstract [FileVisitResult](../../../../reference/java/nio/file/FileVisitResult.html)\n**visitFileFailed**\n(T file, [IOException](../../../../reference/java/io/IOException.html) exc)\n\nInvoked for a file that could not be visited. This method is invoked\nif the file's attributes could not be read, the file is a directory\nthat could not be opened, and other reasons. \n\n##### Parameters\n\n| file | a reference to the file |\n| exc | the I/O exception that prevented the file from being visited |\n|------|--------------------------------------------------------------|\n\n##### Returns\n\n- the visit result \n\n##### Throws\n\n| [IOException](../../../../reference/java/io/IOException.html) | if an I/O error occurs |\n|---------------------------------------------------------------|------------------------|"]]