Java : SimpleFileVisitor con ejemplos

SimpleFileVisitor (Java SE 22 & JDK 22) en Java con ejemplos.
Encontrará muestras de código para la mayoría de los métodos SimpleFileVisitor<T>.

Nota :


Summary

Un visitante simple de archivos con comportamiento predeterminado para visitar todos los archivos y volver a generar errores de E/S. (Traducción automática)

Class diagram

// A file tree.

// --- PowerShell ---
//PS R:\java-work> tree /F
//...
//R:.
//└─target-dir
//    ├─dir1
//    │      aaa.txt
//    │      bbb.txt
//    │
//    └─dir2
//            ccc.txt
//            ddd.txt

// An example to delete a target directory with contents.
final var visitor = new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(
            Path file, BasicFileAttributes attrs) throws IOException {
        System.out.println("visitFile          : " + file);

        Files.delete(file);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult postVisitDirectory(
            Path dir, IOException exc) throws IOException {
        System.out.println("postVisitDirectory : " + dir);

        if (exc != null) {
            throw exc;
        }

        Files.delete(dir);
        return FileVisitResult.CONTINUE;
    }
};

final var targetDir = Path.of("R:", "java-work", "target-dir");
System.out.println("target dir : " + targetDir);
System.out.println("exists : " + Files.exists(targetDir));

System.out.println("-- walkFileTree : start --");
Files.walkFileTree(targetDir, visitor);

System.out.println("-- walkFileTree : end --");
System.out.println("exists : " + Files.exists(targetDir));

// Result
// ↓
//target dir : R:\java-work\target-dir
//exists : true
//-- walkFileTree : start --
//visitFile          : R:\java-work\target-dir\dir1\aaa.txt
//visitFile          : R:\java-work\target-dir\dir1\bbb.txt
//postVisitDirectory : R:\java-work\target-dir\dir1
//visitFile          : R:\java-work\target-dir\dir2\ccc.txt
//visitFile          : R:\java-work\target-dir\dir2\ddd.txt
//postVisitDirectory : R:\java-work\target-dir\dir2
//postVisitDirectory : R:\java-work\target-dir
//-- walkFileTree : end --
//exists : false

See also : Delete a directory with contents


Constructors

SimpleFileVisitor ()

Inicializa una nueva instancia de esta clase. (Traducción automática)

// --- PowerShell ---
//PS R:\java-work> tree /F
//...
//R:.
//│  aaa.txt
//│  bbb.txt
//│
//├─dir1
//│  │  ccc.txt
//│  │  ddd.txt
//│  │
//│  └─dir1-2
//│          eee.txt
//│          fff.txt
//│
//└─dir2
//        ggg.txt
//        hhh.txt

final var start = Path.of("R:", "java-work");
System.out.println("start dir : " + start);

final var visitor = new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
        System.out.println("preVisitDirectory  : " + dir);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
        System.out.println("visitFile          : " + file);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
        System.out.println("postVisitDirectory : " + dir);
        return FileVisitResult.CONTINUE;
    }
};

System.out.println("-- walkFileTree : start --");
Files.walkFileTree(start, visitor);

System.out.println("-- walkFileTree : end --");

// Result
// ↓
//start dir : R:\java-work
//-- walkFileTree : start --
//preVisitDirectory  : R:\java-work
//visitFile          : R:\java-work\aaa.txt
//visitFile          : R:\java-work\bbb.txt
//preVisitDirectory  : R:\java-work\dir1
//visitFile          : R:\java-work\dir1\ccc.txt
//visitFile          : R:\java-work\dir1\ddd.txt
//preVisitDirectory  : R:\java-work\dir1\dir1-2
//visitFile          : R:\java-work\dir1\dir1-2\eee.txt
//visitFile          : R:\java-work\dir1\dir1-2\fff.txt
//postVisitDirectory : R:\java-work\dir1\dir1-2
//postVisitDirectory : R:\java-work\dir1
//preVisitDirectory  : R:\java-work\dir2
//visitFile          : R:\java-work\dir2\ggg.txt
//visitFile          : R:\java-work\dir2\hhh.txt
//postVisitDirectory : R:\java-work\dir2
//postVisitDirectory : R:\java-work
//-- walkFileTree : end --
final var start = Path.of("R:", "java-work");
System.out.println("start dir : " + start);

final var visitor = new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
        System.out.println("preVisitDirectory  : " + dir);

        if (Path.of("dir1").equals(dir.getFileName())) {
            System.out.println("                     dir1 SKIP!");
            return FileVisitResult.SKIP_SUBTREE;
        }

        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
        System.out.println("visitFile          : " + file);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
        System.out.println("postVisitDirectory : " + dir);
        return FileVisitResult.CONTINUE;
    }
};

System.out.println("-- walkFileTree : start --");
Files.walkFileTree(start, visitor);

System.out.println("-- walkFileTree : end --");

// Result
// ↓
//start dir : R:\java-work
//-- walkFileTree : start --
//preVisitDirectory  : R:\java-work
//visitFile          : R:\java-work\aaa.txt
//visitFile          : R:\java-work\bbb.txt
//preVisitDirectory  : R:\java-work\dir1
//                     dir1 SKIP!
//preVisitDirectory  : R:\java-work\dir2
//visitFile          : R:\java-work\dir2\ggg.txt
//visitFile          : R:\java-work\dir2\hhh.txt
//postVisitDirectory : R:\java-work\dir2
//postVisitDirectory : R:\java-work
//-- walkFileTree : end --

Methods

FileVisitResult postVisitDirectory (T dir, IOException exc)

Se invoca para un directorio después de que se hayan visitado las entradas del directorio y todos sus descendientes. (Traducción automática)

// --- PowerShell ---
//PS R:\java-work> tree /F
//...
//R:.
//│  aaa.txt
//│  bbb.txt
//│
//├─dir1
//│  │  ccc.txt
//│  │  ddd.txt
//│  │
//│  └─dir1-2
//│          eee.txt
//│          fff.txt
//│
//└─dir2
//        ggg.txt
//        hhh.txt

final var start = Path.of("R:", "java-work");
System.out.println("start dir : " + start);

final var visitor = new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
        System.out.println("preVisitDirectory  : " + dir);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
        System.out.println("visitFile          : " + file);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
        System.out.println("postVisitDirectory : " + dir);
        return FileVisitResult.CONTINUE;
    }
};

System.out.println("-- walkFileTree : start --");
Files.walkFileTree(start, visitor);

System.out.println("-- walkFileTree : end --");

// Result
// ↓
//start dir : R:\java-work
//-- walkFileTree : start --
//preVisitDirectory  : R:\java-work
//visitFile          : R:\java-work\aaa.txt
//visitFile          : R:\java-work\bbb.txt
//preVisitDirectory  : R:\java-work\dir1
//visitFile          : R:\java-work\dir1\ccc.txt
//visitFile          : R:\java-work\dir1\ddd.txt
//preVisitDirectory  : R:\java-work\dir1\dir1-2
//visitFile          : R:\java-work\dir1\dir1-2\eee.txt
//visitFile          : R:\java-work\dir1\dir1-2\fff.txt
//postVisitDirectory : R:\java-work\dir1\dir1-2
//postVisitDirectory : R:\java-work\dir1
//preVisitDirectory  : R:\java-work\dir2
//visitFile          : R:\java-work\dir2\ggg.txt
//visitFile          : R:\java-work\dir2\hhh.txt
//postVisitDirectory : R:\java-work\dir2
//postVisitDirectory : R:\java-work
//-- walkFileTree : end --
final var start = Path.of("R:", "java-work");
System.out.println("start dir : " + start);

final var visitor = new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
        System.out.println("preVisitDirectory  : " + dir);

        if (Path.of("dir1").equals(dir.getFileName())) {
            System.out.println("                     dir1 SKIP!");
            return FileVisitResult.SKIP_SUBTREE;
        }

        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
        System.out.println("visitFile          : " + file);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
        System.out.println("postVisitDirectory : " + dir);
        return FileVisitResult.CONTINUE;
    }
};

System.out.println("-- walkFileTree : start --");
Files.walkFileTree(start, visitor);

System.out.println("-- walkFileTree : end --");

// Result
// ↓
//start dir : R:\java-work
//-- walkFileTree : start --
//preVisitDirectory  : R:\java-work
//visitFile          : R:\java-work\aaa.txt
//visitFile          : R:\java-work\bbb.txt
//preVisitDirectory  : R:\java-work\dir1
//                     dir1 SKIP!
//preVisitDirectory  : R:\java-work\dir2
//visitFile          : R:\java-work\dir2\ggg.txt
//visitFile          : R:\java-work\dir2\hhh.txt
//postVisitDirectory : R:\java-work\dir2
//postVisitDirectory : R:\java-work
//-- walkFileTree : end --

FileVisitResult preVisitDirectory (T dir, BasicFileAttributes attrs)

Se invoca para un directorio antes de que se visiten las entradas del directorio. (Traducción automática)

// --- PowerShell ---
//PS R:\java-work> tree /F
//...
//R:.
//│  aaa.txt
//│  bbb.txt
//│
//├─dir1
//│  │  ccc.txt
//│  │  ddd.txt
//│  │
//│  └─dir1-2
//│          eee.txt
//│          fff.txt
//│
//└─dir2
//        ggg.txt
//        hhh.txt

final var start = Path.of("R:", "java-work");
System.out.println("start dir : " + start);

final var visitor = new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
        System.out.println("preVisitDirectory  : " + dir);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
        System.out.println("visitFile          : " + file);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
        System.out.println("postVisitDirectory : " + dir);
        return FileVisitResult.CONTINUE;
    }
};

System.out.println("-- walkFileTree : start --");
Files.walkFileTree(start, visitor);

System.out.println("-- walkFileTree : end --");

// Result
// ↓
//start dir : R:\java-work
//-- walkFileTree : start --
//preVisitDirectory  : R:\java-work
//visitFile          : R:\java-work\aaa.txt
//visitFile          : R:\java-work\bbb.txt
//preVisitDirectory  : R:\java-work\dir1
//visitFile          : R:\java-work\dir1\ccc.txt
//visitFile          : R:\java-work\dir1\ddd.txt
//preVisitDirectory  : R:\java-work\dir1\dir1-2
//visitFile          : R:\java-work\dir1\dir1-2\eee.txt
//visitFile          : R:\java-work\dir1\dir1-2\fff.txt
//postVisitDirectory : R:\java-work\dir1\dir1-2
//postVisitDirectory : R:\java-work\dir1
//preVisitDirectory  : R:\java-work\dir2
//visitFile          : R:\java-work\dir2\ggg.txt
//visitFile          : R:\java-work\dir2\hhh.txt
//postVisitDirectory : R:\java-work\dir2
//postVisitDirectory : R:\java-work
//-- walkFileTree : end --
final var start = Path.of("R:", "java-work");
System.out.println("start dir : " + start);

final var visitor = new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
        System.out.println("preVisitDirectory  : " + dir);

        if (Path.of("dir1").equals(dir.getFileName())) {
            System.out.println("                     dir1 SKIP!");
            return FileVisitResult.SKIP_SUBTREE;
        }

        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
        System.out.println("visitFile          : " + file);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
        System.out.println("postVisitDirectory : " + dir);
        return FileVisitResult.CONTINUE;
    }
};

System.out.println("-- walkFileTree : start --");
Files.walkFileTree(start, visitor);

System.out.println("-- walkFileTree : end --");

// Result
// ↓
//start dir : R:\java-work
//-- walkFileTree : start --
//preVisitDirectory  : R:\java-work
//visitFile          : R:\java-work\aaa.txt
//visitFile          : R:\java-work\bbb.txt
//preVisitDirectory  : R:\java-work\dir1
//                     dir1 SKIP!
//preVisitDirectory  : R:\java-work\dir2
//visitFile          : R:\java-work\dir2\ggg.txt
//visitFile          : R:\java-work\dir2\hhh.txt
//postVisitDirectory : R:\java-work\dir2
//postVisitDirectory : R:\java-work
//-- walkFileTree : end --

FileVisitResult visitFile (T file, BasicFileAttributes attrs)

Se invoca para un archivo en un directorio. (Traducción automática)

// --- PowerShell ---
//PS R:\java-work> tree /F
//...
//R:.
//│  aaa.txt
//│  bbb.txt
//│
//├─dir1
//│  │  ccc.txt
//│  │  ddd.txt
//│  │
//│  └─dir1-2
//│          eee.txt
//│          fff.txt
//│
//└─dir2
//        ggg.txt
//        hhh.txt

final var start = Path.of("R:", "java-work");
System.out.println("start dir : " + start);

final var visitor = new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
        System.out.println("preVisitDirectory  : " + dir);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
        System.out.println("visitFile          : " + file);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
        System.out.println("postVisitDirectory : " + dir);
        return FileVisitResult.CONTINUE;
    }
};

System.out.println("-- walkFileTree : start --");
Files.walkFileTree(start, visitor);

System.out.println("-- walkFileTree : end --");

// Result
// ↓
//start dir : R:\java-work
//-- walkFileTree : start --
//preVisitDirectory  : R:\java-work
//visitFile          : R:\java-work\aaa.txt
//visitFile          : R:\java-work\bbb.txt
//preVisitDirectory  : R:\java-work\dir1
//visitFile          : R:\java-work\dir1\ccc.txt
//visitFile          : R:\java-work\dir1\ddd.txt
//preVisitDirectory  : R:\java-work\dir1\dir1-2
//visitFile          : R:\java-work\dir1\dir1-2\eee.txt
//visitFile          : R:\java-work\dir1\dir1-2\fff.txt
//postVisitDirectory : R:\java-work\dir1\dir1-2
//postVisitDirectory : R:\java-work\dir1
//preVisitDirectory  : R:\java-work\dir2
//visitFile          : R:\java-work\dir2\ggg.txt
//visitFile          : R:\java-work\dir2\hhh.txt
//postVisitDirectory : R:\java-work\dir2
//postVisitDirectory : R:\java-work
//-- walkFileTree : end --
final var start = Path.of("R:", "java-work");
System.out.println("start dir : " + start);

final var visitor = new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
        System.out.println("preVisitDirectory  : " + dir);

        if (Path.of("dir1").equals(dir.getFileName())) {
            System.out.println("                     dir1 SKIP!");
            return FileVisitResult.SKIP_SUBTREE;
        }

        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
        System.out.println("visitFile          : " + file);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
        System.out.println("postVisitDirectory : " + dir);
        return FileVisitResult.CONTINUE;
    }
};

System.out.println("-- walkFileTree : start --");
Files.walkFileTree(start, visitor);

System.out.println("-- walkFileTree : end --");

// Result
// ↓
//start dir : R:\java-work
//-- walkFileTree : start --
//preVisitDirectory  : R:\java-work
//visitFile          : R:\java-work\aaa.txt
//visitFile          : R:\java-work\bbb.txt
//preVisitDirectory  : R:\java-work\dir1
//                     dir1 SKIP!
//preVisitDirectory  : R:\java-work\dir2
//visitFile          : R:\java-work\dir2\ggg.txt
//visitFile          : R:\java-work\dir2\hhh.txt
//postVisitDirectory : R:\java-work\dir2
//postVisitDirectory : R:\java-work
//-- walkFileTree : end --

FileVisitResult visitFileFailed (T file, IOException exc)

Se invoca para un archivo que no se pudo visitar. (Traducción automática)

// --- PowerShell ---
//PS R:\java-work> tree /F
//...
//R:.
//├─dir1
//└─dir2
//        aaa.txt
//
//PS R:\java-work> ls | Get-Acl
//
//    Directory: R:\java-work
//
//Path Owner           Access
//---- -----           ------
//dir1 MY-PC\test-user BUILTIN\Administrators Deny  ReadData…
//dir2 MY-PC\test-user BUILTIN\Administrators Allow  FullControl…

// The dir1 directory has read permission denied.

final var start = Path.of("R:", "java-work");
System.out.println("start dir : " + start);

final var visitor = new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
        System.out.println("preVisitDirectory  : " + dir);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
        System.out.println("visitFile          : " + file);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFileFailed(Path file, IOException exc) {
        System.out.println("visitFileFailed    : " + file);
        System.out.println("IOException        : " + exc);
        return FileVisitResult.CONTINUE;
    }
};

System.out.println("-- walkFileTree : start --");
Files.walkFileTree(start, visitor);

System.out.println("-- walkFileTree : end --");

// Result
// ↓
//start dir : R:\java-work
//-- walkFileTree : start --
//preVisitDirectory  : R:\java-work
//visitFileFailed    : R:\java-work\dir1
//IOException        : java.nio.file.AccessDeniedException: R:\java-work\dir1
//preVisitDirectory  : R:\java-work\dir2
//visitFile          : R:\java-work\dir2\aaa.txt
//-- walkFileTree : end --

Related posts

To top of page