Java File IO - List files and directories recursively
- Details
- Written by Nam Ha Minh
- Last Updated on 29 July 2019   |   Print Email
The article How to list files and directories in a directory explains very well about how to list all files and directories under a specific directory, however its examples are only for listing the first level files and directories (those which are directly under the parent directory). But a directory may contain many sub files and sub directories which are nested in many levels, as in the following screenshot, for example:
To list all sub files and sub directories nested in all levels, one solution is to use recursion algorithm:
- List files in the first level.
- For each file in the first level list files:
- If the file is a directory:
- Print out directory name.
- Repeat the step 1 and 2 with the current directory.
- If the file is a file:
- Print out file name.
- Continue with next file.
- If the file is a directory:
The following method implements the above algorithm:
public void listDirectory(String dirPath, int level) { File dir = new File(dirPath); File[] firstLevelFiles = dir.listFiles(); if (firstLevelFiles != null && firstLevelFiles.length > 0) { for (File aFile : firstLevelFiles) { for (int i = 0; i < level; i++) { System.out.print("\t"); } if (aFile.isDirectory()) { System.out.println("[" + aFile.getName() + "]"); listDirectory(aFile.getAbsolutePath(), level + 1); } else { System.out.println(aFile.getName()); } } } }
The listDirectory() method has two parameters:
- dirPath: Absolute path of the directory being listed.
- level: this number is used to indent the output, make the output looks like a hierarchical structure. The method must be invoked with the level = 0.
As you can see, inside the listDirectory() method, there is a call to itself:
listDirectory(aFile.getAbsolutePath(), level + 1);
That’s the point which makes the entire nested sub files and sub directories listed.
Here is a sample program:
import java.io.File; /** * * @author www.codejava.net * */ public class ListDirectoryRecurisve { public void listDirectory(String dirPath, int level) { File dir = new File(dirPath); File[] firstLevelFiles = dir.listFiles(); if (firstLevelFiles != null && firstLevelFiles.length > 0) { for (File aFile : firstLevelFiles) { for (int i = 0; i < level; i++) { System.out.print("\t"); } if (aFile.isDirectory()) { System.out.println("[" + aFile.getName() + "]"); listDirectory(aFile.getAbsolutePath(), level + 1); } else { System.out.println(aFile.getName()); } } } } public static void main(String[] args) { ListDirectoryRecurisve test = new ListDirectoryRecurisve(); String dirToList = System.getProperty("user.home") + File.separator + "Documents"; test.listDirectory(dirToList, 0); } }
The sample program lists all files and directories in the user’s Documents directory. The output looks like as the following screenshot:
Related File IO Tutorials:
- Calculate total files, sub directories and size of a directory
- How to copy a directory programmatically in Java
- How to rename/move file or directory in Java
Other Java File IO Tutorials:
- How to Read and Write Text File in Java
- How to Read and Write Binary Files in Java
- Java IO - Common File and Directory Operations Examples
- Java Serialization Basic Example
- Understanding Java Externalization with Examples
- How to execute Operating System Commands in Java
- 3 ways for reading user's input from console in Java
- File change notification example with Watch Service API
- Java Scanner Tutorial and Code Examples
- How to compress files in ZIP format in Java
- How to extract ZIP file in Java
Comments
Yes, of course. You can modify the above code to return a list of directory objects.