If you don’t know how to search files with specific extensions in a directory, then read this page carefully. Here we have shared the easiest ways to search files with specific extensions using Java code.
Method 1:
If you are using > Java 8, you can use Files.walk()
to find files with specific extensions. Here is the source code in which we have implemented the Files.walk()
method.
Source code:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Example {
public static void main(String[] args) throws IOException {
Path path = Paths.get("E:\\Java Programs");
List paths = FindingFile(path, ".zip");
paths.forEach(y -> System.out.println(y));
}
public static List FindingFile(Path path, String extension){
if (!Files.isDirectory(path)) {
throw new IllegalArgumentException("Path must be a directory!");
}
List result = new ArrayList<>();
try (Stream walk = Files.walk(path)) {
result = walk
.filter(Files::isRegularFile)
.filter(p -> p.getFileName().toString().endsWith(extension))
.collect(Collectors.toList());
} catch(IOException ex){
System.out.println("Error");
}
return result;
}
}
Output:
run:
E:\Java Programs\.metadata\.mylyn\.tasks.xml.zip
E:\Java Programs\.metadata\.mylyn\repositories.xml.zip
E:\Java Programs\Abdul\Fiverr Front End3.zip
E:\Java Programs\Anattele\Game.zip
E:\Java Programs\Bot (2).zip
E:\Java Programs\Bot.zip
E:\Java Programs\Business Management Tool (2).zip
E:\Java Programs\Business Management Tool.zip
E:\Java Programs\CSVUpdatingProgram.zip
E:\Java Programs\DSA Paper.zip
E:\Java Programs\SDA Theory Bot.zip
BUILD SUCCESSFUL (total time: 2 seconds)
Method 2:
Another way for finding files with specific extensions is to use accept()
method of the FilenameFilter
interface. We have created a class that is extended from FilenameFilter
. In this class, we override the accept()
method that returns the file name ending with the extension that the user passes as a parameter.
Source code:
import java.io.File;
import java.io.FilenameFilter;
public class Example {
public void DisplayFiles(String folder, String extension) {
ExtensionFilter filter = new ExtensionFilter(extension);
File direction = new File(folder);
String[] list = direction.list(filter);
for (int i = 0; i < list.length; i++) {
// printing the elements
System.out.println(list[i]);
}
//If array of string is empty, it will return
if (list.length == 0) {
System.out.println("Nothing to show");
return;
}
}
public static void main(String args[]) {
String listOfExtension[]={".PNG",".zip",".JPG"};
String FILE_DIR = "E://Java Programs";
for(int i=0; i<listOfExtension.length; i++)
new Example().DisplayFiles(FILE_DIR, listOfExtension[i]);
}
}
class ExtensionFilter implements FilenameFilter {
private String ext;
public ExtensionFilter(String ext) {
this.ext = ext;
}
@Override
public boolean accept(File dir, String name) {
// This method return the file name along with the file extension
return (name.endsWith(ext));
}
}
Output:
run:
ss.png
Bot (2).zip
Bot.zip
Business Management Tool (2).zip
Business Management Tool.zip
CSVUpdatingProgram.zip
DSA Paper.zip
Mail Room Stack.zip
Queues Assignment.zip
SDA Theory Bot.zip
Task.zip
Nothing to show
BUILD SUCCESSFUL (total time: 2 seconds)
References:
Happy Learning 🙂