31

What would be the fastest way to list the names of files from 1000+ directories and sub-directories?

EDIT; The current code I use is:

import java.io.File;

public class DirectoryReader {

  static int spc_count=-1;

  static void Process(File aFile) {
    spc_count++;
    String spcs = "";
    for (int i = 0; i < spc_count; i++)
      spcs += " ";
    if(aFile.isFile())
      System.out.println(spcs + "[FILE] " + aFile.getName());
    else if (aFile.isDirectory()) {
      System.out.println(spcs + "[DIR] " + aFile.getName());
      File[] listOfFiles = aFile.listFiles();
      if(listOfFiles!=null) {
        for (int i = 0; i < listOfFiles.length; i++)
          Process(listOfFiles[i]);
      } else {
        System.out.println(spcs + " [ACCESS DENIED]");
      }
    }
    spc_count--;
  }

  public static void main(String[] args) {
    String nam = "D:/";
    File aFile = new File(nam);
    Process(aFile);
  }

}
3
  • What do you mean by fastest? simplest to code, best performing on a certain OS, or best performing for certain directory structures? Jun 9, 2010 at 17:02
  • Well in my case; to be fast on WinXP and the simplest possible.
    – Adnan
    Jun 9, 2010 at 17:03
  • Upvoting because this is a common usecase and it certainly helped me Jul 27, 2012 at 7:53

6 Answers 6

36

As this answer shows up on top of google, i'm adding a java 7 nio solution for listing all files and directories, it is takes about 80% less time on my system.

try {
    Path startPath = Paths.get("c:/");
    Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult preVisitDirectory(Path dir,
                BasicFileAttributes attrs) {
            System.out.println("Dir: " + dir.toString());
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            System.out.println("File: " + file.toString());    
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file, IOException e) {
            return FileVisitResult.CONTINUE;
        }
    });
} catch (IOException e) {
    e.printStackTrace();
}
9

This looks fine (Recursively going through the directory) The bottleneck will be all the file i/o you need to do, optimizing your Java will not show any real improvements.

0
5

The only improvement is to get rid of static spc_count and pass spcs string as a parameter to Process.

public static void main(String[] args) {
  String nam = "D:/";
  File aFile = new File(nam);
  Process("", aFile);
}

And when doing recursive call, do

static void Process( String spcs, File aFile) {
  ...
  Process(spcs + " ", listOfFiles[i]);
  ...
}

This way you can call this method from more than 1 thread.

1
  • Thank you for the tip @Alexander Pogrebnyak
    – Adnan
    Jun 9, 2010 at 18:59
4

Until Java 7 introduces the new java.nio.file classes (like DirectoryStream), I'm afraid what you already have will be the fastest.

0
4

If you're open to using a 3rd party library, check out javaxt-core. It includes a multi-threaded recursive directory search that should be faster than iterating through one directory at a time. There are some examples here:

http://www.javaxt.com/javaxt-core/io/Directory/Recursive_Directory_Search

-1

I have written a much simpler code....Try this... It will show every folder, subfolders and files...

 int Files=0,Directory=0,HiddenFiles=0,HiddenDirectory=0;

 public void listf(String directoryName){

File file=new File(directoryName);

File[] fileList=file.listFiles();

if(fileList!=null){

for(int i=0;i<fileList.length;i++){

if(fileList[i].isHidden()){

if(fileList[i].isFile())

{

   System.out.println(fileList[i]);

HiddenFiles++;

}

else{

  listf(String.valueOf(fileList[i]));

  HiddenDirectory++;

}

}

else if (fileList[i].isFile()) {

//System.out.println(fileList[i]);

Files++;

}

else if(fileList[i].isDirectory()){

Directory++;

listf(String.valueOf(fileList[i]));

}

}

}

}



public void Numbers(){

   System.out.println("Files: "+Files+" HiddenFiles: "+HiddenFiles+"Hidden Directories"+HiddenDirectory+" Directories: "+Directory);`

    }  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.