Q:

Java program to traverse all files of a directory/folder

belongs to collection: Java File Handling Programs

0

Given a directory (folder) and we have to traverse its all files using java program.

Example:

Input:
Enter path: 'E:\Java'
(Java folder in 'E' drive)

Output:
About to traverse the directory: JAVA
   About to traverse the directory: 1 Nov
      Visiting file:ChristmasTree.java
      Visiting file:Find Files in Zip File.docx      
   Finished with the directory: 1 Nov
   About to traverse the directory: 2 Nov
      Visiting file:Combine Path.docx
      Visiting file:CombinePath.java
   Finished with the directory: 2 Nov
   About to traverse the directory: 3 Nov
      Visiting file:Check absolute path.docx
      Visiting file:CheckAbsolutePath.java
   Finished with the directory: 2 Nov

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to traverse all files from a directory in java

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Scanner;

public class TraverseFileSystem {
  public static void main(String[] args) {
    try {
      // create object of scanner.
      Scanner KB = new Scanner(System.in);

      // enter path here.
      System.out.print("Enter path here : ");
      String A = KB.next();
      Path path = Paths.get(A);
      ListFiles listFiles = new ListFiles();
      Files.walkFileTree(path, listFiles);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

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(' ');
    }
  }

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

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

  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;
  }

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

Output

Enter path here : E:/JAVA
About to traverse the directory: JAVA
   About to traverse the directory: 1 Nov
      Visiting file:ChristmasTree.java
      Visiting file:Find Files in Zip File.docx      
   Finished with the directory: 1 Nov
   About to traverse the directory: 2 Nov
      Visiting file:Combine Path.docx
      Visiting file:CombinePath.java
   Finished with the directory: 2 Nov
   About to traverse the directory: 3 Nov
      Visiting file:Check absolute path.docx
      Visiting file:CheckAbsolutePath.java
   Finished with the directory: 2 Nov

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java File Handling Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java - Print File Content, Display File... >>
<< Java program to read a file line by line...