Difference between FilenameFilter and FileFilter interfaces

sam_2012
sam_2012 used Ask the Experts™
on
Need help on two interfaces in Java/ I want to know the difference between
FilenameFilter and FileFilter interfaces in Java. Both have the same accept method , but onlky difference being number of and type of parameter in each interface.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
Well the other difference of course is that one is gui-specific. The other, (in java.io) i've found to be slightly more flexible as you don't need to separate the name from the directory
gskTechnology consultant

Commented:
Hi Sam,

The FilenameFilter returns an array of abstract pathnames indicating the files and directories in the directory indicated by this abstract pathname that satisfy the specified filter.when we have to find all files with a certain extension only any do some operations on those files such as deleting them (like file ending with .log or .tmp extension like error.log or  history.tmp etc

TheFileFilter -
where we have to work with a particular type of files inside a directory. “java.io.FileFilter” class is present in IO package for these type of purposes only

public interface FileFilter
{
   public abstract boolean accept(File file);
}

 It contains only one method and this method takes a File instance as parameter and returns true/false whether if file present or not.
IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
@CEHJ,

They are actually both in java.io, not sure which one you think is somewhere else too.

@Sam,

You are correct in that they only differ by their arguments, so that is the reason why you would chose one over the other. If all you want to filter on in the name of the file (which is given as a String) you would probably choose FilenameFilter, e.g. filter for all files starting with a certain prefix using name.startsWith("abc");

However, FileFilter supplies you with the full File object so you could do different/more complex filtering, e.g. filter for all subdirectories using pathname.isDirectory()

In this case, it may be seen as a bit of overkill, to have two interfaces and extra methods to list the files, just to get a different parameter type, when it is very easy to get a File object given the File dir and String name, and vice versa, but that's just how it is.
Top Expert 2016

Commented:
They are actually both in java.io, not sure which one you think is somewhere else too.
Sorry - was thinking of  javax.swing.filechooser.FileFilter

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial