List All Files in a Directory Recursively in Java



The given task is to list all the files from a specific directory, recursively, which means we need to retrieve all the files in the main directory and all the subdirectories as well. For example, if we have a directory structure like below -

              MyDirectory
               /       \
              /         \
            Dir1        Dir2
             /           /  \
            /         Dir21  \  
        File1.txt      /      File12.txt
        File2.txt     /       File22.txt
                    File210.txt

If we have a main directory called MyDirectory, and it has two subdirectories, Dir1 and Dir2, and each of these directories has files and subdirectories, we can list all the files in the main directory and all its subdirectories using Java. The output we want is as follows:

File1.txt
File2.txt
File12.txt
File22.txt
File210.txt

Listing All Files in a Directory in Java

To achieve this, we have various ways. Some of those are as follows:

  • Using File.listFiles()
  • Using Files.walk()

Let's understand each of them in detail.

Using File.listFiles() Method

This method uses the File.listFiles() function to find all files in a folder and its subfolders. The listFiles() function gives us a list of files and folders inside a directory.

Example

Following is the code to list all the files in a directory recursively using the File.listFiles() method:

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

public class ListFilesRecursively {
   public static void main(String[] args) {
      // Create a File object for the directory
      File directory = new File("MyDirectory");
      // Create a List to store the files
      List<File> fileList = new ArrayList<>();
      // Call the method to list all files in the directory recursively
      listFiles(directory, fileList);
      // Print the files
      for (File file : fileList) {
         System.out.println(file.getName());
      }
   }

   public static void listFiles(File directory, List<File> fileList) {
      // Get all the files and directories in the directory
      File[] files = directory.listFiles();
      if (files != null) {
         for (File file : files) {
            if (file.isDirectory()) {
               // If it is a directory, call the method recursively
               listFiles(file, fileList);
            } else {
               // If it is a file, add it to the list
               fileList.add(file);
            }
         }
      }
   }
}

The output of the above code will be:

File1.txt
File2.txt
File12.txt
File22.txt
File210.txt

Using Files.walk() Method

In this method, we will use the Files.walk() method to list all the files in a directory and its subdirectories. The walk() method gives us a stream of file paths, which we can process to get the list of files. The syntax of the walk() method is:

walk(Path start, int maxDepth, FileVisitOption... options);

Where:

  • start - the starting path
  • maxDepth - the maximum number of directory levels to visit
  • options - the options to configure the walk
  • Path - the path to the directory
  • Stream - the stream of paths
  • FileVisitOption - the options to configure the walk

Example

Following is the code to list all the files in a directory recursively using the Files.walk() method:

import java.io.*;
import java.nio.file.*;
import java.util.stream.*;

public class ListFilesRecursively {
   public static void main(String[] args) {
      // Create a Path object for the directory
      Path directory = Paths.get("MyDirectory");
      try (Stream<Path> paths = Files.walk(directory)) {
         // List all files in the directory recursively
         paths.filter(Files::isRegularFile)
              .forEach(System.out::println);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

The output of the above code will be:

MyDirectory/Dir1/File1.txt
MyDirectory/Dir1/File2.txt
MyDirectory/Dir2/File12.txt
MyDirectory/Dir2/File22.txt
MyDirectory/Dir2/Dir21/File210.txt
Updated on: 2025-05-12T10:04:59+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements