Last Updated:

Java FilenameFilter. We get a list of files with a specific extension

The interface can be implemented to filter file names in a specific folder. The interface contains the . The class must implement this method, and each file to be tested must be included in the general list of files.

java.io.FilenameFilterFilenameFilterboolean accept(File dir, String name)

Example of a list of files with a specific extension

Let's use to find all files of a certain extension in a specific folder.

java.io.FilenameFilter

The class implements the . The method checks the file name for a specific extension. In the method, we call the method , which is used to define the files in XML format.

MyFileNameFilterFilenameFilteracceptmain()findFilesMyFileNameFilter

The result of the program:

Note that the class is written in such a way that it ignores the case when checking the file extension.

MyFileNameFilter