File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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
 
Similar Threads
FilenameFilter Interface!!!!!
How to get a list of files with specified suffix
The method list(FilenameFilter) in the type File is not applicable for the arguments (FilenameFilter
Reg. File.list() method...
Check if a file exist in a directory without its extension