[Java] Get the file path in the folder with List

A method that tries to make a list of files in the argument folder into a String List.

FileUtils



import java.io.File;
import java.util.LinkedList;
import java.util.List;

public class FileUtils {

	/**
	 *Returns the Path of the folder or file in the argument directory as a string List
	 *
	 * @param root
	 * @return
	 */
	public static List<String> getFileList(String root) {
		return getFileList(new File(root));
	}

	/**
	 *
	 *Returns the Path of the folder or file in the argument directory as a string List
	 *
	 * @param root
	 * @return
	 */
	public static List<String> getFileList(File root) {
		return getFileList(root, null);
	}

	/**
	 *
	 *Returns the Path of the folder or file in the argument directory as a string List
	 *
	 * @param root
	 * @param fileList
	 * @return
	 */
	private static List<String> getFileList(File root, List<String> fileList) {
		//Returns null if the argument folder does not exist
		if (root == null || !root.exists())
			return null;
		if (fileList == null)
			fileList = new LinkedList<>();
		if (root.isDirectory())
			for (File f : root.listFiles())
				fileList.addAll(getFileList(f, null));
		else
			fileList.add(root.getPath());
		return fileList;
	}
}

Recommended Posts

[Java] Get the file path in the folder with List
[Java] Get the file in the jar regardless of the environment
Unzip the zip file in Java
Get the result of POST in Java
[Java] Get List / Map elements with Iterator
Get the public URL of a private Flickr file in Java
Read xlsx file in Java with Selenium
How to get the date in java
[Java8] Search the directory and get the file
How to get the length of an audio file in java
[Java] Get the date with the LocalDateTime class
For the time being, run the war file delivered in Java with Docker
`SAXReader`: Get the xml file with the default namespace set in XPath format
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]
Read a string in a PDF file with Java
Measure the size of a folder in Java
[JAVA] Get only the file name, excluding the extension
[Java] Read the file in src / main / resources
[Java] How to get the authority of the folder
Get along with Java containers in Cloud Run
[java] sort in list
Get the path defined in Controller class of Spring boot as a list
Get the URL of the HTTP redirect destination in Java
Include image in jar file with java static method
[Java] How to extract the file name from the path
Source used to get the redirect source URL in Java
Display the list in setDetails on the screen with spring-security
[Java] Get the date 10 days later with the Calendar class
Get started with serverless Java with the lightweight framework Micronaut!
Get EXIF information in Java
[Java] Get KClass in Java [Kotlin]
List aggregation in Java (Collectors.groupingBy)
In Redmine you can get the project with Project.find (<identifier>)
Get attributes and values from an XML file in Java
About the behavior when doing a file map with java
Filter the Java No. 2 Optional list that was useful in business and get the first value
Access the network interface in Java
[Java] Delete the elements of List
Guess the character code in Java
Read Java properties file in C #
[Java1.8 +] Get the date of the next x day of the week with LocalDate
[Java] Get the dates of the past Monday and Sunday in order
Find out the list of fonts available in AWS Lambda + Java
Get Null-safe Map values in Java
[Java] How to search for a value in an array (or list) with the contains method
Log output to file in Java
Follow the link with Selenium (Java)
Place in the middle with css
List of members added in Java 9
Parsing the COTOHA API in Java
Examine the list of timezone IDs available in the Java ZoneId class
EXCEL file update sample with JAVA
[Java] I want to perform distinct with the key in the object
About file copy processing in Java
Get stuck in a Java primer
List of types added in Java 9
Play with Markdown in Java flexmark-java
[Java] How to convert from String to Path type and get the path
Overwrite upload of file with the same name with BOX SDK (java)
Java8 list conversion with Stream map