Difference between FilenameFilter and FileFilter interfaces

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.
sam_2012Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
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
0
gskTechnology consultantCommented:
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.
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
@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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.