Walking the File Tree

sanjiaowuhuan發表於2020-12-24

Walking the File Tree

The FileVisitor Interface

PrintFiles.java

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
import static java.nio.file.FileVisitResult.*;

public class PrintFiles extends SimpleFileVisitor<Path> {

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {

        if (attr.isSymbolicLink()) {

            System.out.format("Symbolic link: %s ", file);

        } else if (attr.isRegularFile()) {

            System.out.format("Regular file: %s ", file);

        } else {

            System.out.format("other: %s ", file);

        }

        System.out.println("(" + attr.size() + "bytes)");

        return CONTINUE;

    }

    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException e) {

        System.out.format("Directory: %s%n", dir);

        return CONTINUE;

    }

    @Override
    public FileVisitResult visitFileFailed(Path file, IOException e) {

        System.err.println(e);

        return CONTINUE;

    }

}

kickstarting the Process

KickStart.java

import java.io.*;
import java.nio.file.*;

public class KickStart {

    public static void main(String[] args) {

        Path startingDir = Paths.get("/tmp");

        PrintFiles pf = new PrintFiles();

        try {

            Files.walkFileTree(startingDir, pf);

        } catch (IOException e) {

            System.out.println(e);

        }

    }

}

KickStart2.java

import java.util.*;
import java.io.*;
import java.nio.file.*;

public class KickStart2 {

    public static void main(String[] args) {

        Path startingDir = Paths.get(".");

        PrintFiles pf = new PrintFiles();

        try {

            // Files.walkFileTree(startingDir, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, pf);
            // follow links is show dir in link
            Files.walkFileTree(startingDir, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, pf);

        } catch (IOException e) {

            System.out.println(e);

        }

    }

}

Consideration when Creating a FileVisitor

watch examples

Controlling the Flow

SkipDir.java

import java.nio.file.*;
import java.io.*;
import java.nio.file.attribute.*;
import static java.nio.file.FileVisitResult.*;

public class SkipDir {

    public static void main(String[] args) throws IOException {

        Path start = Paths.get(".");

        Files.walkFileTree(start, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {

                if (dir.getFileName().toString().equals("SCCS")) {

                    return SKIP_SUBTREE;

                }

                return CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path dir, BasicFileAttributes attrs) throws IOException {

                System.out.println("File name: " + dir.getFileName());

                return CONTINUE;

            }

        });

    }

}

LocateFile.java

import java.nio.file.*;
import java.io.*;
import java.nio.file.attribute.*;
import static java.nio.file.FileVisitResult.*;

public class LocateFile {

    public static void main(String[] args) throws IOException {

        Path start = Paths.get(".");

        Path lookingFor = Paths.get("Format.java");

        Files.walkFileTree(start, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

                if (file.getFileName().equals(lookingFor)) {

                    System.out.println("Located file: " + file);

                    return TERMINATE;

                }

                System.out.println("File name: " + file);

                return CONTINUE;

            }

        });

    }

}

Examples