[Java8] Search the directory and get the file


Nice to meet you, I am a first year web engineer who started working as a new graduate in April this year. This is the first post! We will continue to output the skills and skills that we have learned in the future. I would appreciate it if you could comment if you made a mistake in the code or if this is better. Thank you!

Thing you want to do

Create a method (searchFile method) that acquires the path containing a specific file from a directory whose number of layers is unknown and outputs it. Here, search under "C: \ tmp" and return the file path including "hoge" in the file name.


Eclipse Oxygen Java8


  1. Store the directory / file list of the path specified by the listFile method in an array

  2. Take out the acquired contents in a loop, evaluate whether it is a file, and conditional branch a. File → Evaluate whether the file you want b. Others → Recursive call to searchFile method


import java.io.File;

public class Main {
	public static void main(String[] args) {
		//Specify the path you want to search

	public static void searchFile(String targetPath) {
		File dir = new File(targetPath);
        //Directory under the path/Put the files in an array
		File[] list = dir.listFiles();
		for(File path : list) {
			if(path.isFile()) {
				//Divide the obtained path with a backslash"\\"not"\\\\"give
				String[] splitedPath = path.toString().split("\\\\");
				String file = splitedPath[splitedPath.length-1];
				if(file.toString().contains("hoge")) {
			//Recursive method call if file is not a file
			}else {




In order to search all directories for which the number of layers is unknown, I think the point is to make a recursive call that calls itself in a conditional branch.

Recommended Posts

[Java8] Search the directory and get the file
[Java] How to get the current directory
[JAVA] Get only the file name, excluding the extension
[Java] Get the file in the jar regardless of the environment
[Java] Get the file path in the folder with List
Get attributes and values from an XML file in Java
Unzip the zip file in Java
[Java] Get the dates of the past Monday and Sunday in order
Get the public URL of a private Flickr file in Java
Read the first 4 bytes of the Java class file and output CAFEBABE
[Java] How to convert from String to Path type and get the path
How to get the length of an audio file in java
Get the result of POST in Java
[Java] How to use the File class
[Java] Get the day of the specific day of the week
What is the best file reading (Java)
How to get the date in java
[Java 7] Divide the Java list and execute the process
Java: Place the ResourceBundle properties file anywhere
[Java] Get the date with the LocalDateTime class
The story of forgetting to close a file in Java and failing
[Java] How to get the current date and time and specify the display format
[Java] Create a jar file with both compressed and uncompressed with the jar command
Memo: [Java] If a file is in the monitored directory, process it.
How to get the absolute path of a directory running in Java
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
How to create your own annotation in Java and get the value
[Java] Understand the difference between List and Set
Regarding the transient modifier and serialization in Java
JAVA: jar, aar, view the contents of the file
Java: Download the file and save it in the location selected in the dialog [Use HttpClient]
[Java] How to get and output standard input
[Java] Get and display the date 10 days later using the Time API added from Java 8.
[Java] Program example to get the maximum and minimum values from an array
[Java] Get the length of the surrogate pair string
[Java] The confusing part of String and StringBuilder
I compared the characteristics of Java and .NET
[Java] Get images with Google Custom Search API
Summarize the differences between C # and Java writing
[Java] Read the file in src / main / resources
java file creation
Java and JavaScript
How to get and study java SE8 Gold
XXE and Java
Compile and run Java on the command line
[Java] How to get the redirected final URL
java directory creation
[Java] How to get the authority of the folder
[Java] Convert and import file values with OpenCSV
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
[Android development] Get an image from the server in Java and set it in ImageView! !!
[Java] How to get the key and value stored in Map by iterative processing
How to save a file with the specified extension under the directory specified in Java to the list
Form and process file and String data at the same time with Spring Boot + Java
[Java] How to get the URL of the transition source
Get the URL of the HTTP redirect destination in Java
<java> Read Zip file and convert directly to string
Write ABNF in Java and pass the email address
Access the war file in the root directory of Tomcat
Please note the division (division) of java kotlin Int and Int
The comparison of enums is ==, and equals is good [Java]