Monday 20 February 2012

Shows all the files in a directory and its sub directories

Below java program traverse through directory and displays all the files in it.

 
 
package com.test;

import java.io.File;

/**
 * Shows all the files in a directory and its sub directories 
 * @author luckyacademy
 *
 */
public class ListFiles {
 public static void main(String[] args) {

  showFiles(new File("C:\\dell"));
 }

 /**
  * Recursively shows all the files.
  */
 private static void showFiles(File dirPath) {
  
  for (File file : dirPath.listFiles()) {
   if (file.isDirectory()){ //if it is directory, traverse the directory recursively  
    showFiles(file);
   } 
   System.out.println("file Name: " + file.getAbsolutePath());
  }
 }

}

No comments:

Post a Comment