File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes FileNameFilter & FileFilter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "FileNameFilter & FileFilter" Watch "FileNameFilter & FileFilter" New topic

FileNameFilter & FileFilter

Vikrama Sanjeeva
Ranch Hand

Joined: Sep 02, 2001
Posts: 756
I can't understand both of these interfaces.Another thing is that both have accept() method taking different arguments.Why SUN has not included both methods in one interface.
Plz. Explain it.
Count the flowers of ur garden,NOT the leafs which falls away!

Count the flowers of your garden, NOT the leafs which falls away!
Prepare IBM Exam 340 by joining
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Hi Vikrama
Well, the main use of these filters is to selectively list the contents of a given directory. The class has 2 relevant methods for doing this: <code>list</code> (which returns an array of Strings representing the names of the files and directories) and <code>listFiles</code> (which returns an array of File objects).
In both cases, the method is overloaded to allow you to use a filter to only get files you are interested in (e.g. only files with a .txt extension). The listFiles method takes an instance of FileFilter as the argument dictating this behaviour, whereas the list method uses an instance of FilenameFilter to achieve (what is conceptually) the same result.
The logic controlling what is/is not a file to be accepted is left to the user to implement. You may decide to filter files with a certain extension, or greater than a certain size, or you may want only files and not directories, etc, etc. There is therefore obviously a very large collection of possible scenarios for determing whether or not a file should be accepted, and these are best left to the user to decide on a per application basis.
As far as the different arguments - the FileFilter needs only a File object as its argument, since information such as the path to this file and its name can be obtained from the file object itself. The Filename filter uses its File object argument not as an identifier to the file - the second argument (of type String) performs this function. The file argument in this case is the directory in which the file to be filtered was found.
Hopefully that made some sense.
Clear as mud?
"One good thing about music - when it hits, you feel no pain"
Bob Marley

"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
I agree. Here's the link:
subject: FileNameFilter & FileFilter
It's not a secret anymore!