0

I'm using the simple file visitor for the first time and the documentation seems to be a little less than friendly. Basically I want to visit the directory tree and log the number of folders, the number of total files, and the number of different files like, txt, csv, etc that was visited.

This is my FileVistor.

public class FileVisitor extends SimpleFileVisitor<Path>{


    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
        System.out.println("About to scan " + dir);
        return FileVisitResult.CONTINUE;
    }

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

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


        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
        System.err.println(exc.getMessage());
        return FileVisitResult.CONTINUE;
    }


}

And this is my calling code.

Scanner scanner = new Scanner(System.in);
    String str = scanner.nextLine();
    Path path = Paths.get(str);
    FileVisitor visitor = new FileVisitor();
    Files.walkFileTree(path, visitor);

Any help appreciated.

Zeus
  • 2,213
  • 8
  • 28
  • 45
  • 1
    What exactly is your question? Where is your problem and what have you tried so far? – Idos Mar 09 '16 at 13:36
  • Copying and pasting a line from the original question. `Basically I want to visit the directory tree and log the number of folders, the number of total files, and the number of different files like, txt, csv, etc that was visited.` – Zeus Mar 09 '16 at 13:42
  • 2
    The point is: where exactly are you struggling?! This is not a site where you drop your requirements and can expect people to then check out your source code to finally tell you what is missing. That is **your** job. We help with concrete questions; not with "here is something, go figure what is missing". – GhostCat Mar 09 '16 at 13:45
  • see this [tutorial](http://javapapers.com/java/walk-file-tree-with-java-nio/). This Java NIO tutorial is to learn about walking a file tree. – Bacteria Mar 09 '16 at 13:46
  • `Files.walkFileTree` is a JDK 7 method that can be replaced by new, easier-to-use JDK 8 methods from the same class. – Klitos Kyriacou Mar 09 '16 at 13:46

0 Answers0