This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Implementing FilenameFilters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Implementing FilenameFilters" Watch "Implementing FilenameFilters" New topic
Author

Implementing FilenameFilters

Alex Kirk
Ranch Hand

Joined: Aug 13, 2001
Posts: 44
I'm trying to use a FilenameFilter with java.io.File.list(). From reading the API, it's obvious that I have to get an instance of FilenameFilter to implement an accept() method which returns true only once my specified criteria have been met. I understand how to write the accept method to function properly; what I don't get is how to create the class properly.
I thought, after looking at some examples, that I could create a class, myFileFilter.java, that would simply implement FilenameFilter and declare its own accept method, like so:
import java.io.*;
public class dirFileFilter implements FilenameFilter
{
public boolean accept(File theFile, String theName)
{
if (theFile.isDirectory() == true)
return false;
else
return true;
}
}
However, when I tried to use File.list(new dirFileFilter()), I realized that the constructor here returns a dirFileFilter, not a FilenameFilter as File.list() requires.
Do I need to create my own FilenameFilter class? That wouldn't seem to me to make sense, since I'd then be overwriting (for that program) the implementation provided by Java, and thus losing functionality.
Could I create a class that returned a FilenameFilter object with an appropriate accept() method? If so, how?
Thanks in advance for your help.
Alex Kirk
Jason Ford
Ranch Hand

Joined: Aug 02, 2001
Posts: 54
However, when I tried to use File.list(new dirFileFilter()), I realized that the constructor here returns a dirFileFilter, not a FilenameFilter as File.list() requires.

Did you actually try doing it this way? It should work; since dirFileFilter implements FilenameFilter, a dirFileFilter IS A FilenameFilter.


SCJP, SCWCD
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
You have it correct.
When you construct an instance of a dirFileFilter, because it implements FilenameFilter - it IS a FilenameFilter and meets the requirements of File.list().
That is the power of interfaces. ANY class that implements the interface can be used as the input parameter to anything requiring a FilenameFilter.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Alex Kirk
Ranch Hand

Joined: Aug 13, 2001
Posts: 44
Well, you guys had it right. Basically, I'd not implemented FilenameFilter properly (since I didn't have java.io.* imported, the system couldn't find FilenameFilter to implement), so dirFileFilter didn't return right. Once I got that fixed, it was A-OK.
Alex
 
jQuery in Action, 2nd edition
 
subject: Implementing FilenameFilters