• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FileNameFilter & FileFilter

 
Vikrama Sanjeeva
Ranch Hand
Posts: 760
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
Bye
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vikrama
Well, the main use of these filters is to selectively list the contents of a given directory. The java.io.File 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?
Michael
------------------
"One good thing about music - when it hits, you feel no pain"
Bob Marley
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic