Java FilenameFilter Examples

In software development, we need to deals with files. We need to find files of certain extensions line .tmp, .txt, etc and delete the files.

In java, we have FilenameFilter as a functional interface. We need to override its

 accept(File target directory, String fileName) method, to perform the file filtering on all files inside the target directory.

The instances FilenameFilter are used to filter directory listings in the list method of class File, and by the Abstract Window Toolkit's file dialog component.

Method Details

boolean accept(File dir, String name)

Tests if a specified file should be included in a file list.

Parameters:
dir - the directory in which the file was found.
name - the name of the file.

It returns true if and only if the name should be included in the file list; false otherwise.

Here we have an example to filter all files based on file names matching a regular expression.

1). FilenameFilter using Regular Expression

import java.io.File; import java.io.FilenameFilter; public class FilterFileNamesUsingRegex { public static void main(String[] args) { //Delete all files from this directory String targetDir = "D:\\archive"; File dir = new File(targetDir); //Filter out all the files inside archive dir String[] fileNames = dir.list((d, s) - > { return fileNames.matches("[a-zA-z]+\\.[a-z]+"); }); //If no files found; no need to go further if (logFiles.length == 0) return; //do the needful operations for each files. } }

2). Create FilenameFilter using Lambda expression

As we mentioned earlier FilenameFilter is a functional interface. We can apply the lambda expression over this interface.

import java.io.File; import java.io.FilenameFilter; public class FilenameFilterWithLambdaExpression { public static void main(String[] args) { //Delete all files from this directory String targetDirectory = "D:\\archive"; File dir = new File(targetDirectory); String[] pdfFiles = fileterFiles(dir); //do the required operations like move files or delete file } public static String[] fileterFiles(File dir) { String[] pdfFiles = new String[]; //Filter out all pdf files String[] pdfFiles = dir.list((d, s) - >{ return s.toLowerCase().endsWith(".pdf"); }); if (pdfFiles.length == 0) { ///throw exception or log exception no pdf files found. } return pdfFiles; } }

3). Delete the files with given extension from the directory

In the below example we filter out all files with pdf(we can have any extensions like txt, CSV, log, doc ) extensions.

import java.io.FilenameFilter; import java.io.File; public class FilenameFilterExample { public static void main(String[] args) { String targetDir = "D:\\archive"; // /home/archive in case of linux File dir = new File(targetDir); //Filter out all pdf files inside archive directory String[] pdfFiles = dir.list(new pdfFilter()); //If no files found we have to stop here if (pdfFiles.length == 0) { return; } else { //This code will delete all log files one by one for (String pdfFile: pdfFiles) { String tempPdfFile = new StringBuffer(targetDir).append(File.separator).append(pdfFile).toString(); File fileToDelete = new File(tempPdfFile); boolean isDeleted = fileToDelete.delete(); System.out.println("the file : " + tempPdfFile + " is deleted? " + isDeleted); } } }

We are implementing FilenameFilter's accept method to filter out the pdf files

class pdfFilter implements FilenameFilter { @Override public boolean accept(File dir, String fileName) { return (fileName.endsWith(".pdf")); } }

In this article, we have seen Java FilenameFilter Examples.