Abstract Interface FilenameFilter in Java

digitization, transformation, binary

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.

Ads code goes here

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{
//method
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.

READ  Abstract Class SocketImpl in Java

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

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *