65

Possible Duplicate:
List all files from a directory recursively with Java

How can i return a file array that include all files on the folder and also sub folders my method just work for folder and it doesn't include sub folders .

public File[] listf(String directoryName) {

    // .............list file
    File directory = new File(directoryName);

    // get all the files from a directory
    File[] fList = directory.listFiles();

    for (File file : fList) {
        if (file.isFile()) {
            System.out.println(file.getAbsolutePath());
        } else if (file.isDirectory()) {
            listf(file.getAbsolutePath());
        }
    }
    System.out.println(fList);
    return fList;
}                        
Community
  • 1
  • 1
Mostafa Jamareh
  • 1,389
  • 4
  • 22
  • 54

3 Answers3

103

Using you current code, make this tweak:

public void listf(String directoryName, List<File> files) {
    File directory = new File(directoryName);

    // Get all files from a directory.
    File[] fList = directory.listFiles();
    if(fList != null)
        for (File file : fList) {      
            if (file.isFile()) {
                files.add(file);
            } else if (file.isDirectory()) {
                listf(file.getAbsolutePath(), files);
            }
        }
}
Hasen
  • 11,710
  • 23
  • 77
  • 135
Nathan
  • 1,747
  • 1
  • 12
  • 12
38

Use FileUtils from Apache commons.

listFiles

public static Collection<File> listFiles(File directory,
                                         String[] extensions,
                                         boolean recursive)
Finds files within a given directory (and optionally its subdirectories) which match an array of extensions.
Parameters:
directory - the directory to search in
extensions - an array of extensions, ex. {"java","xml"}. If this parameter is null, all files are returned.
recursive - if true all subdirectories are searched as well
Returns:
an collection of java.io.File with the matching files
Skrodde
  • 655
  • 3
  • 7
  • 19
Ajay George
  • 11,759
  • 1
  • 40
  • 48
22

You can return a List instead of an array and things gets much simpler.

    public static List<File> listf(String directoryName) {
        File directory = new File(directoryName);

        List<File> resultList = new ArrayList<File>();

        // get all the files from a directory
        File[] fList = directory.listFiles();
        resultList.addAll(Arrays.asList(fList));
        for (File file : fList) {
            if (file.isFile()) {
                System.out.println(file.getAbsolutePath());
            } else if (file.isDirectory()) {
                resultList.addAll(listf(file.getAbsolutePath()));
            }
        }
        //System.out.println(fList);
        return resultList;
    } 
Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58
  • but i want to merge the array – Mostafa Jamareh Feb 03 '13 at 19:52
  • Why? I mean: what is your problem and why would you want to avoid using lists which are doing all the merging for you? – Cyrille Ka Feb 03 '13 at 19:55
  • yes excuse me i sleepy(sleeplessness) at the moment – Mostafa Jamareh Feb 03 '13 at 20:04
  • this works , i have used it in my app – kinsley kajiva Jul 16 '17 at 18:55
  • This is okay to recursively return files and directories within that specified directory. And as the question (even though its a duplicate) is asking how to return (only) files from the provided directory and files within all sub-directories you may have to change the codes a little bit: //resultList.addAll(Arrays.asList(fList)); for (File file : fList) { if (file.isFile()) { resultList.add(file); } else if (file.isDirectory()) { resultList.addAll(listf(file.getAbsolutePath())); } } – Ishimwe Aubain Consolateur Feb 12 '19 at 09:58