Source Code : Using the SimpleFileVisitor class to traverse file systems

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Using the SimpleFileVisitor class to traverse file systems

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;
import java.nio.file.attribute.BasicFileAttributes;

class ListFiles extends SimpleFileVisitor<Path> {
  private final int indentionAmount = 3;
  private int indentionLevel;

  public ListFiles() {
    indentionLevel = 0;
  }

  private void indent() {
    for (int i = 0; i < indentionLevel; i++) {
      System.out.print();
    }
  }

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

  @Override
  public FileVisitResult postVisitDirectory(Path directory, IOException e)
      throws IOException {
    indentionLevel -= indentionAmount;
    indent();
    System.out.println("Finished with the directory: "
        + directory.getFileName());
    return FileVisitResult.CONTINUE;
  }

  @Override
  public FileVisitResult preVisitDirectory(Path directory,
      BasicFileAttributes attributes) throws IOException {
    indent();
    System.out.println("About to traverse the directory: "
        + directory.getFileName());
    indentionLevel += indentionAmount;
    return FileVisitResult.CONTINUE;
  }

  @Override
  public FileVisitResult visitFileFailed(Path file, IOException exc)
      throws IOException {
    System.out.println("A file traversal error ocurred");
    return super.visitFileFailed(file, exc);
  }
}

public class Test {

  public static void main(String[] args) {
    try {
      Path path = Paths.get("/home");
      ListFiles listFiles = new ListFiles();
      Files.walkFileTree(path, listFiles);
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }
}

 

Thank with us