Java 8 provides a nice stream to process all files in a tree.

try (Stream<Path> stream = Files.walk(Paths.get(path))) {
    stream.filter(Files::isRegularFile)
          .forEach(System.out::println);
}

This provides a natural way to traverse files. Since it's a stream you can do all nice stream operations on the result such as limit, grouping, mapping, exit early etc.

UPDATE: I might point out there is also Files.find which takes a BiPredicate that could be more efficient if you need to check file attributes.

Files.find(Paths.get(path),
           Integer.MAX_VALUE,
           (filePath, fileAttr) -> fileAttr.isRegularFile())
        .forEach(System.out::println);

Note that while the JavaDoc eludes that this method could be more efficient than Files.walk it is effectively identical, the difference in performance can be observed if you are also retrieving file attributes within your filter. In the end, if you need to filter on attributes use Files.find, otherwise use Files.walk, mostly because there are overloads and it's more convenient.

TESTS: As requested I've provided a performance comparison of many of the answers. Check out the Github project which contains results and a test case.

Answer from Brett Ryan on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › list-all-files-from-a-directory-recursively-in-java
List all Files from a Directory Recursively in Java - GeeksforGeeks
April 28, 2025 - For directories, repeat step 2 recursively. Use the directory path as the new starting point for exploring its subdirectories and their files. Java program to List Files from a Directory using the Files class to list all files recursively:
🌐
CodeJava
codejava.net › java-se › file-io › list-files-and-directories-recursively-in-a-directory
Java File IO - List files and directories recursively
July 29, 2019 - 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
🌐
TutorialsPoint
tutorialspoint.com › java-program-to-list-all-files-in-a-directory-recursively
Java program to List all files in a directory recursively
May 12, 2025 - 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 ...
🌐
Baeldung
baeldung.com › home › java › java io › listing files recursively with java
Listing Files Recursively With Java | Baeldung
August 18, 2024 - This method uses Apache Commons IO’s FileUtils.iterateFiles() to create an iterator over all files in a directory, then prints each file’s absolute path. Additionally, second parameter of this method allows us to filter files by its extension (e.g: {“java”, “xml”}). In this case it is defaulted to null, so no files are filtered out. And with the last parameter we can easily decide if we want this iteration to be recursive for all subdirectories.
Top answer
1 of 1
10
Recursively Listing All Files Under a Directory in Java Recursively listing all files within a directory and its subdirectories is a common task in Java programming. This process involves navigating through each folder and its nested folders to retrieve and display all the files contained within. Understanding how to implement this efficiently can aid in tasks such as file management, data processing, and building applications that require file system interactions. Steps to Recursively List All Files 1. Choose the Right Approach In Java, there are multiple ways to traverse directories recursively. You can use traditional recursion with the File class or leverage the more modern java.nio.file package introduced in Java 7. 2. Using the File Class with Recursion The File class provides methods to interact with the file system. Here's how you can use it to list all files recursively: Example Code import java.io.File; public class FileLister { public static void main(String[] args) { String directoryPath = "C:\\Path\\To\\Your\\Directory"; File directory = new File(directoryPath); listFilesRecursively(directory); } public static void listFilesRecursively(File dir) { if (dir.isDirectory()) { File[] files = dir.listFiles(); if (files != null) { // Check for permission issues for (File file : files) { if (file.isDirectory()) { listFilesRecursively(file); // Recursive call for subdirectories } else { System.out.println(file.getAbsolutePath()); } } } } else { System.out.println(dir.getAbsolutePath()); } } }
🌐
Blogger
javarevisited.blogspot.com › 2021 › 05 › how-to-recursively-list-all-files-in-java.html
How to recursively show all files in a directory and sub-directory in Java - Example
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.
Find elsewhere
🌐
Quickprogrammingtips
quickprogrammingtips.com › java › recursively-listing-files-in-a-directory-in-java.html
Recursively Listing Files in a Directory in Java
The following method uses a recursive method to list all files under a specific directory tree. The isFile() method is used to filter out all the directories from the list. We iterate through all folders, however we will only print the files encountered. If you are running this example on a ...
🌐
Javaprogramto
javaprogramto.com › 2019 › 12 › java-list-all-files-recursively.html
Java List or Traverse All Files in Folder Recursively (Java 8 Files.walk() Example) JavaProgramTo.com
December 28, 2019 - A quick practical java example program to list all the files and folders in a directory with subfolders (Java 8 Example).
🌐
Makeinjava
makeinjava.com › home › recursively list all files & folders of input directory in java (example)
Recursively list or print all files & folders of input directory in java (with example)
January 2, 2024 - We will use listFiles method of File class to get all files or folders (of current directory). Then, we will print all contents of input directory. package org.learn; import java.io.File; public class ListFilesRecursively { public static void main(String[] args) { String currentDirectory = ...
🌐
sqlpey
sqlpey.com › java › java-list-files-recursively
Java: How to List Files in a Directory Recursively - sqlpey
July 25, 2025 - It iterates through the files in a directory, and if a subdirectory is encountered, it calls itself on that subdirectory. import java.io.File; public class RecursiveFileList { public static void listFilesForFolder(final File folder) { // Iterate through all files and subdirectories in the current folder File[] filesInFolder = folder.listFiles(); if (filesInFolder != null) { // Null check for safety for (final File fileEntry : filesInFolder) { // If it's a directory, recurse into it if (fileEntry.isDirectory()) { listFilesForFolder(fileEntry); } else { // If it's a file, print its name System.out.println("File: " + fileEntry.getName()); } } } } public static void main(String[] args) { // Specify the directory path final File startFolder = new File("/home/you/Desktop"); // Example path listFilesForFolder(startFolder); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-list-all-files-in-a-directory-and-nested-sub-directories
Java Program to List all Files in a Directory and Nested Sub-Directories - GeeksforGeeks
July 23, 2025 - For main directory files, we use foreach loop. ... // Recursive Java program to print all files // in a folder(and sub-folders) import java.io.File; public class GFG { static void RecursivePrint(File[] arr, int level) { // for-each loop for ...
🌐
GitHub
gist.github.com › KazWolfe › f0ae26a7f0c5a5827d87b696834ed7b5
Recursively list directories/files through Java · GitHub
Save KazWolfe/f0ae26a7f0c5a5827d87b696834ed7b5 to your computer and use it in GitHub Desktop. Download ZIP · Recursively list directories/files through Java · Raw · Recurse.java · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
Javapractices
javapractices.com › topic › TopicAction.do
Java Practices->Recursive file listing
* * @param args - <tt>args[0]</tt> is the full name of an existing * directory that can be read. */ public static void main(String... args) throws FileNotFoundException { File startingDirectory= new File(args[0]); FileListing listing = new FileListing(); List<File> files = listing.getFileListing(startingDirectory); //print out all file names, in the the order of File.compareTo() for(File file : files){ System.out.println(file); } } /** * Recursively walk a directory tree and return a List of all * Files found; the List is sorted using File.compareTo().
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › listing all files in a directory in java
Listing All Files in a Directory in Java
October 1, 2022 - Learn to use various Java APIs such as Files.list() and DirectoryStream to list all files present in a directory, including hidden files, recursively.
🌐
Mkyong
mkyong.com › home › java › java – how to list all files in a directory?
Java - How to list all files in a directory? - Mkyong.com
December 25, 2018 - try (Stream<Path> walk = Files.walk(Paths.get("C:\\projects"))) { List<String> result = walk.map(x -> x.toString()) .filter(f -> f.contains("HeaderAnalyzer.java")) .collect(Collectors.toList()); result.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } In the old days, we can create a recursive loop to implement the search file function like this :