This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implementing FilenameFilters

 
Alex Kirk
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alex Kirk
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic