This week's book giveaway is in the Spring forum.
We're giving away four copies of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 and have Marten Deinum & Iuliana Cosmina on-line!
See this thread for details.
Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Implementing FilenameFilters

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic