Abstract Interface FilenameFilter in Java

An instance of classes that implement this interface are used to filter filenames. These instances are used to filter directory listing in the list method of class File(Class File, Utility) and by the abstract window toolkit’s file dialog components.

The FilenameFilter interface is implemented by a class that wants to filter the filenames that
should be included in a list of filenames. For example, the list() method of the File class can take a FilenameFilter object to filter the filenames that are listed. The java.awt.FileDialog class also uses a FilenameFilter to limit the choices that are presented to the user.

This method defines the accept() method that needs to be implemented by any object that filters filenames. There are no standard FinenameFilter classes in java but objects that implement this interface are used by the FileDialog object or File.list() method. It checks for the file or the extension.

So this object can be passed to File.list(), FileDialog.setFilenameFilter(), FileDialogPeer.setFilenameFilet() these method also this object is returned by FileDialog.getFilenameFilter().

The structure of the Interface FilenameFilter is given as:

public abstract interface java.io.FilenameFilter{
public abstract boolean accept(File dir,String name);
//Determines whether a specified file should be included in a file list.
//Returns true if the name should be included in file list,false otherwise.

The details of the class structure are given as follows:

public abstract boolean accept(File dir,String name);

public abstract boolean accept(File dir,String name) method returns a boolean value that indicates whether or not a file should be included in a list of filenames. The method should return true if a file should be included; otherwise it should return false. A simple filter might return true for filenames with a certain extension, like .java. A more complex filter could check the directory name, the file’s readability, and last modification time, for example.

This method returns true if the file should be shown; false otherwise.
dir – The directory that contains the file.
name -The name of the file.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on [email protected]

Write A Comment