Java example to filter files in a directory using FilenameFilter

Introduction :

Sometimes we need to filter out file list based on its extension. For example, filtering out all .mp3 files in a specific folder. To implement such scenarios in Java, we have one inbuilt interface known as FilenameFilter. In this tutorial, we will learn how to use FilenameFilter to filter out files in a specific directory.

FilenameFilter :

FilenameFilter interface is defined as :

public interface FilenameFilter

It contains the following method :

boolean accept(File dir, String name)

Here, dir is the directory where the file was found and name is the name of the file. It will return true if the condition satisfies and false otherwise. For example, if we are looking for .mp3 files and the file name is of .mp3 extension. Then it will return true.
FilenameFilter is passed to the listFiles method :

public File[] listFiles(FilenameFilter filter)

It returns an array of abstract pathnames denoting the files and directories in the directory we are scanning. That means it will list the files for those the provided FilenameFilter returns true. If the FilenameFilter is null, it will return all files and folders.

FilenameFilter example :

import java.io.File;
import java.io.FilenameFilter;

public class Main {
    private static final String PATH = "C:\\user\\Desktop\\files";

    public static void main(String[] args) {
        File directory = new File(PATH);
        File[] fileList = directory.listFiles(new MyFilter());

        for (File file : fileList) System.out.println(file.getName());
    }

}

class MyFilter implements FilenameFilter {
    @Override
    public boolean accept(File dir, String name) {
        return name.endsWith(".mp3");
    }
}

In this program, we are checking folder files that contain the following files :

anotherSong.mp3	firstSong.mp3	new.pdf
file.cbr	in.txt		secondSong.mp3

java filenamefilter

As you can see that it contains a different type of files. If we run the program, MyFilter will filter out the files with .mp3 extension and print out the below output :

anotherSong.mp3
firstSong.mp3
secondSong.mp3

As you can see the mp3 files are filtered out by the FilenameFilter.
Instead of writing a new class for FilenameFilter, we can use the anonymous class :

import java.io.File;
import java.io.FilenameFilter;

public class Main {
    private static final String PATH = "C:\\user\\Desktop\\files";

    public static void main(String[] args) {
        File directory = new File(PATH);
        File[] fileList = directory.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".mp3");
            }
        });

        for (File file : fileList) System.out.println(file.getName());
    }

}

It will print the same output. You can create a new class for FilenameFilter if you want to use the same Filenamefilter in a different part of your program.

java filenamefilter

Using Lambda :

We can convert the above program with a lambda :

import java.io.File;
public class Main {
    private static final String PATH = "C:\\user\\Desktop\\files";

    public static void main(String[] args) {
        File directory = new File(PATH);
        File[] fileList = directory.listFiles((dir, name) -> name.endsWith(".mp3"));

        for (File file : fileList) System.out.println(file.getName());
    }
}

The output will be the same. The main advantage of using lambda is that we don’t require to write down the implementation of FilenameFilter.

java filenamefilter

Conclusion :

FilenameFilter has a lot of advantages. To implement it, you can either use one separate class, anonymous function or lambda. Most people prefer the lambda approach but sometimes using a separate class is useful if you want to use the same filter on different places in your code.

Leave a Reply