In this tutorial, you will learn how to create a File filter in Java. A file filter in this case is a program to filter text files in a directory.
There are different approaches to achieve this in Java. The techniques covered in this tutorial include:
- Using the
FileFilter
interface of Java IO - Using the
newDirectoryStream()
method ofFiles
class. - Using the
suffixFileFilter()
method ofFileFilterUtils
class.
The first two methods come from core Java packages: Java IO and Java NIO. The last method is from the Commons IO library.
Create a new project
You should create a Maven project for this tutorial. Use the following link which provides a detailed article on creating a Maven project.
Add dependencies
After creating the project, copy and paste the following XML into the file named pom.xml.
<!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.13.0</version> </dependency>
Press CTRL+SHIFT+O to reload the Maven changes. This will download the Commons IO dependency and add it to the project’s classpath.
Create a directory and add files
Create a folder named files under the root directory of your project. Next, create the files: file-one.txt, file-two.txt, and file-three.md under the new folder. Note that we have created two text files and one Markdown file.
Your project should be as shown below.

The examples use the files folder to filter the text files in it. As a result, all the examples should display the file names of text files and exclude the Markdown file.
Using the FileFilter interface of Java IO
Create a file named UsingJavaIO.java under the package src/main/java/com/javawhizz. Copy and paste the following code into the file.
package com.javawhizz; import java.io.File; import java.io.FileFilter; import java.util.Arrays; public class UsingJavaIO { public static void main(String[] args) { File folder = new File("./files"); File[] files = folder.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.getName().toLowerCase().endsWith(".txt"); } }); Arrays.stream(files) .forEach(file -> System.out.println(file.getName())); } }
In this example, you will leverage the FileFilter
interface of Java IO to filter text files. This interface contains a method named accept()
that returns a boolean value.
In this case, if a file is a text file, the method will return true
. Otherwise, the method will return false
. As a result, the listFiles()
method will return the name of the path for each of the files matched.
If you run this program, you will note that it returns two text files and omits the Markdown file.
Output
file-two.txt file-one.txt
Using the newDirectoryStream() method of the Files class
Create a file named UsingJavaNIO.java under the package src/main/java/com/javawhizz. Next, copy and paste the following code into the file.
package com.javawhizz; import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class UsingJavaNIO { public static void main(String[] args) { Path folder = Paths.get("./files"); try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(folder, "*.txt");){ directoryStream.forEach( file -> System.out.println(file.getFileName()) ); }catch (IOException e){ e.printStackTrace(); } } }
Output
file-two.txt file-one.txt
This example is quite identical to the previous one. In this example, the program uses the Paths.get()
method to get the path of the directory.
The Files class of the Java NIO package has a method named newDirectoryStream()
. This method returns a Path
Stream
for each text file in the directory.
To filter the files, call this method then pass the Path
object of the folder as the first argument. In the second argument, pass the file extension that you want to match.
In this case, the file extension should be .txt
. Each Path
object represents a concrete path for the file on your machine. Next, use the forEach()
method to view the names of the files in the directory.
Since you are filtering text files, the result should be the same as the one in the previous example.
Using the suffixFileFilter() method of FileFilterUtils class
Create a file named UsingCommonsIO.java under the package src/main/java/com/javawhizz. Next, copy and paste the following code into the file.
package com.javawhizz; import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.commons.io.filefilter.IOFileFilter; import java.io.File; import java.util.Collection; public class UsingCommonsIO { public static void main(String[] args) { File folder = new File("./files"); IOFileFilter ioFileFilter = FileFilterUtils .suffixFileFilter(".txt"); Collection<File> files = FileUtils .listFiles(folder, ioFileFilter, null); files.forEach(file -> System.out.println(file.getName())); } }
The Commons IO package comes into place in this example. This library has a class named FileFilterUtils
that provides static methods.
You can leverage the static methods when working with files. In this example, you will use the suffixFileFilter()
method to filter text files.
This method will return a filter and the filter will in turn return true if the file named ends with the text passed.
This library also has another class named FileUtils that you can use when working with files. In this example, call the listFiles()
method from the class and pass the expected arguments.
The first argument for the method is the folder to search in. For the second argument, pass the filter to apply to the directory. The last argument is for the sub-directories.
Since your directory has no sub-directories, pass null in this case. After running your application you should see the following output.
Output
file-two.txt file-one.txt
Conclusion
In this tutorial, you have learned how to create a File Filter in Java. The steps covered include using the FileFilter
interface of Java IO, using newDirectoryStream()
, and suffixFileFilter()
methods.
Happy hacking!
0 Comments