When it is necessary to operate a large number of files in Java, it is consistently easier to implement the listing part in Java than to list it with the find
command of the terminal and then pass it to Java. So I wrote this code.
This program takes the directory name and extension as arguments, searches the specified directory and the directories below it, and saves the file with the specified extension in ʻArrayList`. Here, find the m4a file in the iTunes folder and save it in the list.
The code above uses the new Java 8 lambda expressions (Lambda Expressions
) and streams (Stream
). See below for code that doesn't use lambda expressions and streams. (It may work with Java older than Java8.)
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.stream.Stream;
public class Utils {
public static void main(String[] args) throws IOException {
// To list up m4a files in iTunes directory
Path rootDir = Paths.get(System.getProperty("user.home"), "Music", "iTunes");
String extension = "m4a";
// for standard inputs
if (args.length == 2) {
rootDir = Paths.get(args[0]);
extension = args[1];
} else if (args.length != 0){
System.err.println("Error: set correct args.");
}
if (!Pattern.matches("^[0-9a-zA-Z]+$", extension)) {
System.err.println("Error: set correct extension. (only alphabet and numeric)");
} else {
ArrayList<File> fileList = listUpFiles(rootDir, extension);
for (File file : fileList) {
System.out.println(file.getAbsolutePath());
}
System.out.println(fileList.size());
}
}
/**
* list up files with specified file extension (extension)
* under the specified directory (rootDir) recursively.
* @param rootDir : root directory
* @param extension : string needs to be contained as file extension
* @return : list of files
*/
public static ArrayList<File> listUpFiles(Path rootDir, String extension){
String extensionPattern = "." + extension.toLowerCase();
final ArrayList<File> fileList = new ArrayList();
try (final Stream<Path> pathStream = Files.walk(rootDir)) {
pathStream
.map(path -> Path::toFile)
.filter(file -> !file.isDirectory())
.filter(file -> file.getName().toLowerCase().endsWith(extensionPattern))
.forEach(fileList::add);
} catch (final IOException e) {
e.printStackTrace();
}
return (fileList);
}
}
import java.io.File;
import java.util.regex.Pattern;
import java.util.ArrayList;
public class UtilsOld {
public static void main(String[] args) {
// To list up m4a files in iTunes directory
String rootDirName = System.getProperty("user.home") + java.io.File.separator +
"Music" + java.io.File.separator + "iTunes";
String extension = "m4a";
// for standard input
if (args.length == 2) {
rootDirName = args[0];
extension = args[1];
} else if (args.length != 0){
System.err.println("Error: set correct args.");
}
if (!Pattern.matches("^[0-9a-zA-Z]+$", extension)) {
System.err.println("Error: set correct extension. (only alphabet and numeric)");
} else {
String extensionPattern = "." + extension.toLowerCase();
ArrayList<File> fileList = new ArrayList();
fileList = listUpFiles(rootDirName, extensionPattern, fileList);
for (File file : fileList) {
System.out.println(file.getAbsolutePath());
}
System.out.println(fileList.size());
}
}
/**
* list up files with specified file extension (ext)
* under the specified directory (rootDirName) recursively.
*
* @param rootDirName : root directory name
* @param extensionPattern : regular expression pattern of file extension
* @param fileArrayList : ArrayList to store files
* @return : return fileArrayList
*/
public static ArrayList listUpFiles(String rootDirName, String extensionPattern, ArrayList<File> fileArrayList){
File rootDir = new File(rootDirName);
File[] listOfFiles = rootDir.listFiles();
assert listOfFiles != null;
for (File item : listOfFiles) {
if (item.isDirectory()) {
fileArrayList = listUpFiles(item.getAbsolutePath(), extensionPattern, fileArrayList);
} else if (item.isFile()) {
String fileName = item.getName().toLowerCase();
if (fileName.endsWith(extensionPattern)) {
fileArrayList.add(item);
}
}
}
return(fileArrayList);
}
}
Comment from saka1029 https://www.javacodegeeks.com/2014/05/playing-with-java-8-lambdas-paths-and-files.html
Recommended Posts