Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Monday, April 24, 2023

How to recursively show all files in a directory and sub-directory in Java - Example

Hello, guys if you are looking for a Java program to recursively list all files in a directory or sub-directory in Java then you have come to the right place. Earlier, I have shared the best Java Programming courses and In this article, I will show you how you can use the SimpleFileVistor class from java.nio package to recursive list all files and directories inside a given directory. The API also provides you control over whether to list only files or directories when you are navigating recursively. This makes it very useful for archiving programs or creating build tools like Maven or any Java program which has to deal with files and directories. 

How to recursively list all files in a directory or its sub-directory in Java

Here is our complete Java program to list all files and directories from a given Path. In this program, I will show you two ways to recursively find all files and directories. In the first method, you will list both files and directories, while in the second example you will list files. 

If you want to learn more about Java File API, I highly recommend you to join a comprehensive Java course like The Complete Java Programming Masterclass by Tim Buchalaka and his team on Udemy. This 80+ hour course is one of the best and most up-to-date courses to learn Java for beginners. 

How to recursively list all files in a directory and sub-directory in Java

Java Program to Recursively Show all Files in a Directory

Now, let's jump into the code to see things in action:
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

* Java Program to recursively list all files inside a directory or its
* sub-directory in Java.
* @author Javin Paul
public class Testing {

    public static void main(String args[]) {
        System.out.println("Recursively listing all files using 
                               JDK 7 walkFileTree() method");

        System.out.println("Recursively list all files only in Java,
                              Alternative way");
        listFilesOnly(new File("target"));


    public static void walkDirectory(String path) {
        Path dir = Paths.get(path);
        try {
            Files.walkFileTree(dir, new ListFileVisitor());
        } catch (IOException e) {

    public static void listFilesOnly(File root) {
        if (root.isDirectory()) {
            File[] allFilesAndDirectories = root.listFiles();
            for (File f : allFilesAndDirectories) {
        } else {

class ListFileVisitor extends SimpleFileVisitor {

    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                     throws IOException {
        return FileVisitResult.CONTINUE;

Recursively listing all files using JDK 7 walkFileTree() method

Recursively list all files only in Java, Alternative way

Files.walkFileTree() in Java

If you provide a starting point and a file visitor, it will invoke various methods on the file visitor as it walks through the file in the file tree. Java programmers should use this if they are developing a recursive copy, a recursive move, a recursive delete, or a recursive operation that sets permissions or performs another operation on each of the files.

If you like to use third-party open-source utility libraries like Apache Commons-IO then you can also use their FileUtils class which has iterateFiles() and listFiles() methods. Instead of using these methods, you can use them as well. 

And, if you just want to list all files recursively with no filtering,  you can also use the FileUtils.listFiles(dir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE), where dir is a File object that points to the base directory You might want to consider using listFilesAndDirs(), as listFiles() does not return empty folders

How to recursively list all files in a directory and sub-directory in Java

That's all about how to recursively list all files in a given directory and their sub-directories in Java. The java.nio package has some powerful tools and classes to interact with the local file system and they are also enhanced significantly on Java 7. You can use them to write powerful tools and utility in Java.

Thank you for reading this Java File IO tutorial. If you have any doubt or questions then please ask in comments section. Happy to answer any doubt you may have.

No comments :

Post a Comment