Recursively list all files present in directory /sub-directories of input folder (java /example)

  1. Given an input directory of given file system in java.
  2. We would like to list all files present in a given directory.
    • i.e. List all files present in current directory or sub directories.
  3. We will recursively traverse directories/ sub directories to list all files.
  4. In trailing code, we will get current directory of user.
  5. Then, we will recursively traverse all directories/sub-directories of input folder using java.

Program – recursively list files present in directory /sub-directories in java

package org.learn;

import java.io.File;

public class ListFilesOnly {
    private static int serialNumber = 0;

    public static void main(String[] args) {
        String currentDirectory = System.getProperty("user.dir");
        System.out.println("Listing files recursively: "+currentDirectory);
        listFilesOnly(new File(currentDirectory));
    }

    private static void listFilesOnly(File inputFile) {
        File[] listFiles = inputFile.listFiles();
        for(File file: listFiles) {
            if(file.isDirectory()) {
                listFilesOnly(file);
            } else {
                System.out.printf("%d. File Path=%s\n", ++serialNumber, file.getAbsolutePath());
            }
        }
    }
}

Output – recursively list files in a directory /sub-directories (java/ example).

Listing files recursively: C:\data
1. File Path=C:\data\db\collection-0-2086694220802165662.wt
2. File Path=C:\data\db\collection-103--5255221449412732250.wt
3. File Path=C:\data\db\collection-105--5255221449412732250.wt
4. File Path=C:\data\db\collection-108--5255221449412732250.wt
5. File Path=C:\data\db\collection-110--5255221449412732250.wt
6. File Path=C:\data\db\collection-113--5255221449412732250.wt
7. File Path=C:\data\db\collection-119--5255221449412732250.wt
8. File Path=C:\data\db\collection-121--5255221449412732250.wt
9. File Path=C:\data\db\collection-123--5255221449412732250.wt
10. File Path=C:\data\db\collection-125--5255221449412732250.wt
11. File Path=C:\data\db\collection-127--5255221449412732250.wt
12. File Path=C:\data\db\collection-131--5255221449412732250.wt
13. File Path=C:\data\db\collection-133--5255221449412732250.wt
14. File Path=C:\data\db\collection-161--5255221449412732250.wt
15. File Path=C:\data\db\collection-163--5255221449412732250.wt
16. File Path=C:\data\db\collection-165--5255221449412732250.wt
17. File Path=C:\data\db\collection-167--5255221449412732250.wt
18. File Path=C:\data\db\collection-171--5255221449412732250.wt
19. File Path=C:\data\db\collection-174--5255221449412732250.wt
20. File Path=C:\data\db\collection-176--5255221449412732250.wt
21. File Path=C:\data\db\collection-178--5255221449412732250.wt
22. File Path=C:\data\db\collection-181--5255221449412732250.wt
23. File Path=C:\data\db\collection-183--5255221449412732250.wt
24. File Path=C:\data\db\collection-185--5255221449412732250.wt
25. File Path=C:\data\db\collection-187--5255221449412732250.wt
26. File Path=C:\data\db\collection-190--5255221449412732250.wt
27. File Path=C:\data\db\collection-195--5255221449412732250.wt
28. File Path=C:\data\db\collection-197--5255221449412732250.wt
29. File Path=C:\data\db\collection-199--5255221449412732250.wt
30. File Path=C:\data\db\collection-81--5255221449412732250.wt
Scroll to Top