[JAVA] I want to recursively search for files under a specific directory

I recently had to recursively search for files under a directory. I think that Java standard api has such a function, but it is not surprising.

__2016-12-29 Addendum ___: Although it is written that "it is not in the standard", it has been pointed out that there are actually various things. See the comments section for details.

For example, suppose you have a directory with the following structure:

D:\>tree D:\temp /f
List of folder paths:Volume Data
Volume serial number is C4C7-10BC
│ Text 0.txt
├─ Folder 1
│ │ Text 1.txt
│  │
│ └─ Folder 1-1
│ Text 1-1.txt
├─ Folder 2
│ Text 1.txt
└─ Folder 3
Text 3.txt

To recursively search for files under this D: \ temp:

package folder;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class FolderUtils {

	public static void main(String[] args) {
		String absolutePath = "D:\\temp";
		List<File> files = FolderUtils.findAllFile(absolutePath);
		for (File file : files) System.out.println(file.getAbsolutePath());
			// D:\temp\Folder 2\Text 1.txt
			// D:\temp\Folder 1\Folder 1-1\Text 1-1.txt
			// D:\temp\Folder 1\Text 1.txt
			// D:\temp\Folder 3\Text 3.txt
			// D:\temp\Text 0.txt


	 *Recursively search for files under a given directory.
	 * @param absolutePath The absolute path of the directory.
	 * @List of return files
	public static List<File> findAllFile(String absolutePath) {

		List<File> files = new ArrayList<>();

		Stack<File> stack = new Stack<>();
		stack.add(new File(absolutePath));
		while (!stack.isEmpty()) {
			File item = stack.pop();
			if (item.isFile()) files.add(item);

			if (item.isDirectory()) {
				for (File child : item.listFiles()) stack.push(child);

		return files;

The algorithm is depth-first search. If it's a directory, it's as simple as searching under it, otherwise it's listed. I chose to implement using a stack instead of a recursive call that is compatible with depth-first search-not because I aimed for an efficient implementation-but because I just wanted to know that Java has a stack (I wanted to use it). ´ ・ ω ・ ) I'm sorry I don't have a big reason (´ ・ ω ・)

Recommended Posts

I want to recursively search for files under a specific directory
I want to recursively search the class list under the package
I want to monitor a specific file with WatchService
I want to create a generic annotation for a type
I want to make a specific model of ActiveRecord ReadOnly
I want to create a chat screen for the Swift chat app!
I want to develop a web application!
I want to write a nice build.gradle
I want to write a unit test!
When you want Rails to disable a session for a specific controller only
I made a Diff tool for Java files
[Java] Processes all files under a certain directory
[Ruby] I want to do a method jump!
I want to simply write a repeating string
I want to design a structured exception handling
I want to delete files managed by Git
[Android] I want to create a ViewPager that can be used for tutorials
I want to recursively get the superclass and interface of a certain class
I want to go back to a specific VC by tapping the back button on the NavigationBar!
I want to call a method of another class
I made a method to ask for Premium Friday
I want to use a little icon in Rails
I want to apply ContainerRelativeShape only to specific corners [SwiftUI]
I want to define a function in Rails Console
I want to add a reference type column later
I want to click a GoogleMap pin in RSpec
Use java1.7 (zulu7) under a specific directory with jenv
I want to connect to Heroku MySQL from a client
I want to add a delete function to the comment function
Implemented a strong API for "I want to display ~~ on the screen" with simple CQRS
[Java] I want to convert a byte array to a hexadecimal number
I want to find a relative path in a situation using Path
I want to implement a product information editing function ~ part1 ~
I want to make a list with kotlin and java!
I want to call a method and count the number
I want to make a function with kotlin and java!
I want to create a form to select the [Rails] category
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to easily back up files used at work
I want to give a class name to the select attribute
I want to create a Parquet file even in Ruby
I want to use FireBase to display a timeline like Twitter
I want to return multiple return values for the input argument
I want to convert characters ...
[Controller] I want to retrieve the numerical value of a specific column from the DB (my memo)
I want to make a button with a line break with link_to [Note]
I want to add a browsing function with ruby on rails
I want to use swipeback on a screen that uses XLPagerTabStrip
Only this I want to remember, 4 designs for writing unit tests
The story of Collectors.groupingBy that I want to keep for posterity
I want to avoid OutOfMemory when outputting large files with POI
[Ruby] I want to put an array in a variable. I want to convert to an array
I made a method to ask for Premium Friday (Java 8 version)
I want to display the images under assets/images in the production environment
[Introduction to JSP + Servlet] I played with it for a while ♬
I want to extract between character strings with a regular expression
[For beginners] I want to automatically enter pre-registered data in the input form with a selection command.
I want to use FormObject well
I want to convert InputStream to String
I want to docker-compose up Next.js!
[Rails] I learned about migration files! (Adding a column to the table)