Starting the Recursive Process - Java File Path IO

Java examples for File Path IO:Directory

Description

Starting the Recursive Process

Demo Code

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;

public class Main {
  public static void main(String[] args) throws Exception {
    Path listDir = Paths.get("C:/folder1"); // define the starting file tree
    ListTree walk = new ListTree(); // instantiate the walk

    try {/*w ww  .  j  a v  a2  s  .co m*/
      Files.walkFileTree(listDir, walk); // start the walk
    } catch (IOException e) {
      System.err.println(e);
    }
  }
}

class ListTree extends SimpleFileVisitor<Path> {

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

    System.out.println("Visited directory: " + dir.toString());

    return FileVisitResult.CONTINUE;
  }

  @Override
  public FileVisitResult visitFileFailed(Path file, IOException exc) {
    System.out.println(exc);

    return FileVisitResult.CONTINUE;
  }
}

Result


Related Tutorials