131

Possible Duplicate:
Best way to iterate through a directory in java?

I want to process each file in a certain directory using Java.

What is the easiest (and most common) way of doing this?

Community
  • 1
  • 1
John Assymptoth
  • 8,227
  • 12
  • 49
  • 68
  • 2
    Duplicate of: http://stackoverflow.com/questions/3154488/best-way-to-iterate-through-a-directory-in-java – Olhovsky Feb 07 '11 at 00:55
  • 1
    This is a duplicate, but not of that question about deep traversal ("including files in all the subdirectories). See instead [How do I iterate through the files in a directory in Java?](https://stackoverflow.com/questions/3154488/how-do-i-iterate-through-the-files-in-a-directory-in-java) – Andy Thomas Mar 27 '20 at 20:19

4 Answers4

219

If you have the directory name in myDirectoryPath,

import java.io.File;
...
  File dir = new File(myDirectoryPath);
  File[] directoryListing = dir.listFiles();
  if (directoryListing != null) {
    for (File child : directoryListing) {
      // Do something with child
    }
  } else {
    // Handle the case where dir is not really a directory.
    // Checking dir.isDirectory() above would not be sufficient
    // to avoid race conditions with another process that deletes
    // directories.
  }
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • 1
    Javado says in listFiles(): "Pathnames denoting the directory itself and the directory's parent directory are not included in the result." – pihentagy Sep 14 '12 at 10:35
  • 1
    fyi if you want the current folder you can use `new File(".");' – SüniÚr Oct 28 '14 at 08:59
35

I guess there are so many ways to make what you want. Here's a way that I use. With the commons.io library you can iterate over the files in a directory. You must use the FileUtils.iterateFiles method and you can process each file.

You can find the information here: http://commons.apache.org/proper/commons-io/download_io.cgi

Here's an example:

Iterator it = FileUtils.iterateFiles(new File("C:/"), null, false);
        while(it.hasNext()){
            System.out.println(((File) it.next()).getName());
        }

You can change null and put a list of extentions if you wanna filter. Example: {".xml",".java"}

Software Craftsman
  • 2,999
  • 2
  • 31
  • 47
jomaora
  • 1,656
  • 3
  • 17
  • 26
  • 3
    @john-assymptoth, without this lib and just the java implemented util you would drive in `StackOverflowError` if the library contains a lot of files. (http://java.sun.com/javase/6/docs/api/java/lang/StackOverflowError.html) – Rihards Apr 15 '11 at 18:51
  • 1
    In the most recent version (2.6) of `commons.io` your call would look like `FileUtils.iterateFiles(new File("C:/"), null, null)` (ignoring subdirectories) or for example `FileUtils.iterateFiles(new File("C:/"), new SuffixFileFilter(".java"), null)` to apply a filter on the file extension. – jammartin Mar 27 '19 at 17:11
11

Here is an example that lists all the files on my desktop. you should change the path variable to your path.

Instead of printing the file's name with System.out.println, you should place your own code to operate on the file.

public static void main(String[] args) {
    File path = new File("c:/documents and settings/Zachary/desktop");

    File [] files = path.listFiles();
    for (int i = 0; i < files.length; i++){
        if (files[i].isFile()){ //this line weeds out other directories/folders
            System.out.println(files[i]);
        }
    }
}
WuHoUnited
  • 8,279
  • 3
  • 24
  • 27
4

Use java.io.File.listFiles
Or
If you want to filter the list prior to iteration (or any more complicated use case), use apache-commons FileUtils. FileUtils.listFiles

Amol Katdare
  • 6,740
  • 2
  • 33
  • 36
  • `listFiles` is overridden to take a file filter or a filename filter so there's no need to use apache-commons if the only thing you want is filtering. Although it is a fine library. – Mike Samuel Feb 07 '11 at 01:34