-
- All Known Implementing Classes:
- SimpleFileVisitor
public interface FileVisitor<T>
A visitor of files. An implementation of this interface is provided to theFiles.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; } });
- Since:
- 1.7
-
-
Method Summary
Methods 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.
-
-
-
Method Detail
-
preVisitDirectory
FileVisitResult preVisitDirectory(T dir, BasicFileAttributes attrs) throws IOException
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 returnsSKIP_SUBTREE
orSKIP_SIBLINGS
then entries in the directory (and any descendants) will not be visited.- Parameters:
dir
- a reference to the directoryattrs
- the directory's basic attributes- Returns:
- the visit result
- Throws:
IOException
- if an I/O error occurs
-
visitFile
FileVisitResult visitFile(T file, BasicFileAttributes attrs) throws IOException
Invoked for a file in a directory.- Parameters:
file
- a reference to the fileattrs
- the file's basic attributes- Returns:
- the visit result
- Throws:
IOException
- if an I/O error occurs
-
visitFileFailed
FileVisitResult visitFileFailed(T file, IOException exc) throws IOException
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 fileexc
- the I/O exception that prevented the file from being visited- Returns:
- the visit result
- Throws:
IOException
- if an I/O error occurs
-
postVisitDirectory
FileVisitResult postVisitDirectory(T dir, IOException exc) throws IOException
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 avisitFile
method returningSKIP_SIBLINGS
, or an I/O error when iterating over the directory).- Parameters:
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- Returns:
- the visit result
- Throws:
IOException
- if an I/O error occurs
-
-
Traduction non disponible
Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Version en cache
06/11/2024 00:23:23 Cette version de la page est en cache (à la date du 06/11/2024 00:23:23) afin d'accélérer le traitement. Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la dernère version de la page.Document créé le 11/06/2005, dernière modification le 04/03/2020
Source du document imprimé : https://www.gaudry.be/java-api-rf-java/nio/file/FileVisitor.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.