Java : SimpleFileVisitor - API Examples

SimpleFileVisitor (Java SE 18 & JDK 18) API Examples.
You will find code examples on most SimpleFileVisitor methods.


Summary

A simple visitor of files with default behavior to visit all files and to re-throw I/O errors.

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

Initializes a new instance of this class.

Please see postVisitDirectory(T dir, IOException exc).

Methods

FileVisitResult postVisitDirectory (T dir, IOException exc)

Invoked for a directory after entries in the directory, and all of their descendants, have been visited.

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

Invoked for a directory before entries in the directory are visited.

Please see postVisitDirectory(T dir, IOException exc).

FileVisitResult visitFile (T file, BasicFileAttributes attrs)

Invoked for a file in a directory.

Please see postVisitDirectory(T dir, IOException exc).

FileVisitResult visitFileFailed (T file, IOException exc)

Invoked for a file that could not be visited.

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

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