HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / I/O / Java FileFilter Example

Java FileFilter Example

Java java.io.FileFilter class is a filter that can be used to returns abstract pathnames denoting the files and directories in a given directory.

1. FileFilter class

The FileFilter class is a filter for abstract pathnames. It has only a single method that tests whether or not the specified abstract pathname should be included in a pathname list.

It returns true if and only if pathname should be included in the list.

boolean	accept(File pathname)

2. Using FileFilter with File class

The best way to use the FileFilter is to pass it to listFiles() method in File class where File represents a directory location:

  • File[] listFiles(FileFilter filter): returns an array of abstract pathnames denoting the files and directories that satisfy the specified filter.

3. Java FileFilter Example

Example 1: Creating a FileFilter instance

The given accept() method check for the file extension; if it is .log then it is considered log file and method returns true otherwise method returns false.

//create a FileFilter and override its accept-method
FileFilter logFilefilter = new FileFilter() 
{
	  //Override accept method
	  public boolean accept(File file) {
			 
			 //if the file extension is .log return true, else false
			 if (file.getName().endsWith(".log")) {
				return true;
			 }
			 return false;
	  }
};

Example 2: Java program to find all log files in a directory using FileFilter class

import java.io.File;
import java.io.FileFilter;

public class IOUtils
{
   public static void main(String[] args)
   {
      IOUtils ioUtils = new IOUtils();
      ioUtils.getFiles("C:\\temp");
   }

   public void getFiles(String dir)
   {
      File directory = new File(dir);

      //Verify if it is a valid file name
      if (!directory.exists())
      {
         System.out.println(String.format("Directory %s does not exist", dir));
         return;
      }

      //Verify if it is a directory and not a file path
      if (!directory.isDirectory())
      {
         System.out.println(String.format("Provided value %s is not a directory", dir));
         return;
      }

      File[] files = directory.listFiles(logFilefilter);

      //Let's list out the filtered files
      for (File f: files)
      {
         System.out.println(f.getName());
      }
   }

   //create a FileFilter and override its accept-method
   FileFilter logFilefilter = new FileFilter() {
      //Override accept method
      public boolean accept(File file) {
         //if the file extension is .log return true, else false
         if (file.getName().endsWith(".log")) {
            return true;
         }
         return false;
      }
   };
}

Program Output:

test.log
test1.log

Above program will list down all .log files present in c:/temp folder.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Ramu

    August 7, 2014

    returns an array of abstract pathnames indicating the files and directories in the directory indicated by this abstract pathname that satisfy the specified filter.

Comments are closed on this article!

Search Tutorials

Java IO

  • Java IO Introduction
  • Java How IO works?
  • Java IO vs NIO
  • Java Create File
  • Java Write to File
  • Java Append to File
  • Java Read File
  • Java Read File to String
  • Java Read File to Byte[]
  • Java Make File Read Only
  • Java Copy File
  • Java Copy Directory
  • Java Delete Directory
  • Java Current Working Directory
  • Java Read/Write Properties File
  • Java Read File from Resources
  • Java Read File from Classpath
  • Java Read/Write UTF-8 Data
  • Java Check if File Exist
  • Java Create Temporary File
  • Java Write to Temporary File
  • Java Delete Temporary File
  • Java Read from Console
  • Java Typesafe input using Scanner
  • Java Password Protected Zip
  • Java Unzip with Subdirectories
  • Java Generate SHA/MD5
  • Java Read CSV File
  • Java InputStream to String
  • Java String to InputStream
  • Java OutputStream to InputStream
  • Java InputStreamReader
  • Java BufferedReader
  • Java FileReader
  • Java LineNumberReader
  • Java StringReader
  • Java FileWriter
  • Java BufferedWriter
  • Java FilenameFilter
  • Java FileFilter

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces