0

I want to get the list of all documents present in the folder. Including the ones present in the folder's sub-folders. Currently am able to write a code only iterate through the documents present in the folder but not it's sub folders.

     File dir = new File("C:\\Users\\...\\Forms");
     File[] list = dir.listFiles();
     for(int i=0; i<list.length;i++)
     {
     ...
     }

Is there a java code for a way to get all the documents from the folder including it's subfolders ?

Obito Uchiha
  • 113
  • 1
  • 8
  • 1
    Well, a `File` might also represent a directory so you could use recursion to get all files in subfolders - that's how many of us do it or at least did it. – Thomas Mar 26 '19 at 11:30
  • 1
    possible duplicate of https://stackoverflow.com/questions/2534632/list-all-files-from-a-directory-recursively-with-java – cquezel Mar 26 '19 at 11:32
  • A Java8+ solution might be to use [`Files.walk(...)`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html#walk(java.nio.file.Path,java.nio.file.FileVisitOption...)). From the Javadoc: "Return a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file." – Thomas Mar 26 '19 at 11:33

2 Answers2

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

You can call the above method as

        ArrayList<File> f = new ArrayList<File>(); 
        listf("C://Users/xyz", f);
        System.out.print(f.size()); // This is the list of files you want.
Shariq
  • 513
  • 4
  • 14
0
You Can use the below code!!

package model;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class iterateThroghFolder {
    public iterateThroghFolder() {
        super();
    }

    static List<File> file = new ArrayList<File>();
    static List<File> list = new ArrayList<File>();;

    public static void main(String[] args) {
        File file = new File("C:/Users/c_umashe/Desktop/Test"); //Required Path

        boolean b = file.isDirectory();
        if (b) {
            iterateOver(file);
        } else{
             list.add(file); 
        }
        System.out.println(list.size());

    }


    public static void iterateOver(File file) {
        File[] innerFiles = file.listFiles();
        int size = innerFiles.length;
        for (int i = 0; i < size; i++) {           
            if (innerFiles[i].isFile()) {            
                    list.add(innerFiles[i]);           
            } else { //If it is a directory
                iterateOver(innerFiles[i]);
            }
        }
    }
}
Uday Kumar
  • 123
  • 10