Keine Cache-Version


Caching deaktiviert Standardeinstellung für diese Seite:aktiviert (code LNG204)
Wenn die Anzeige zu langsam ist, können Sie den Benutzermodus deaktivieren, um die zwischengespeicherte Version anzuzeigen.
java.nio.file

Interface FileVisitor<T>

  • All Known Implementing Classes:
    SimpleFileVisitor

    public interface FileVisitor<T>
    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;
                 }
             });
     
    Since:
    1.7
    • 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 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
        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 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

        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 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

Deutsche Übersetzung

Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.

Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.

Vielen Dank im Voraus.

Dokument erstellt 11/06/2005, zuletzt geändert 04/03/2020
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/java-api-rf-java/nio/file/filevisitor.html

Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.

Referenzen

  1. Zeigen Sie - html-Dokument Sprache des Dokuments:fr Manuel PHP : https://docs.oracle.com

Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.

Inhaltsverzeichnis Haut