Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: FileFilter

Reply
Thread Tools

Re: FileFilter

 
 
Sebastian Hoehn
Guest
Posts: n/a
 
      06-29-2003
On Sat, 28 Jun 2003 23:37:54 +0000, Meir wrote:

> Hi,
>
> Isn't there an easy way to filter files
> according to their name/extension ?
>
> I understand that I must create a FileFilter,
> or FilenameFilter, and it's tedious.
>
> I need a simple file filter.
> I just want to be able to retrieve "myFolder/*.xml" .


That's rather simple, just extend FileFilter:

public MyFilter extends FileFilter {
public boolean accept(File f) {
// Here you decide whether you accept the file:
if (f.getAbsolutePath().equals(...)) {
return true;
}
return false;
}
}

Hope that helps.

- Sebastian
 
Reply With Quote
 
 
 
 
Bhun
Guest
Posts: n/a
 
      06-29-2003
"Sebastian Hoehn" <(E-Mail Removed)>, Sun, 29 Jun 2003 10:25:02 +0200:

> On Sat, 28 Jun 2003 23:37:54 +0000, Meir wrote:
>
>> Hi,
>>
>> Isn't there an easy way to filter files
>> according to their name/extension ?
>>
>> I understand that I must create a FileFilter,
>> or FilenameFilter, and it's tedious.

Why is it tedious? You can also do an in-line declaration of the
FileFilter depending on what you prefer, and save it as a macro somewhere
in your editor.
e.g.

void somemethod()
{
FileFilter xmlFilter = new FileFilter()
{
public
boolean accept(File file)
{
return
file.getAbsolutePath().toLowerCase().endsWith(".xm l");
}
}
...
}

The only downside is that you will have lots of anonymous classes and
possibly lots of code duplication. But this could be negligible on the
whole. There's also a difference in class loading mechanisms when using
anonymous inner classes. From what I've seen the loading of in-line declared
classes are delayed up till the point they are actually encountered.

>> I need a simple file filter.
>> I just want to be able to retrieve "myFolder/*.xml" .

>
> That's rather simple, just extend FileFilter:
>
> public MyFilter extends FileFilter {

Probably a typo or something, FileFilter is an interface. so it should
read:

public class MyFilter implements FileFilter

> public boolean accept(File f) {
> // Here you decide whether you accept the file:
> if (f.getAbsolutePath().equals(...)) {
> return true;
> }
> return false;
> }
> }


Making a reusable FileFilter isn't so hard. This one just check the
suffix, another implementation (perhaps more useful) would be to
incorporate regular expression matching in the FileFilter.

// minimal implementation

public
class SuffixFileFilter
implements FileFilter
{
protected
Set suffixSet;

public
SuffixFileFilter(final String[] suffices)
{
suffixSet = new TreeSet();

for (Iterator suffix=Arrays.asList(suffices).iterator(); suffix.hasNext()
{
suffixSet.add(suffix.next().toString().toLowerCase ().trim());
}

suffixSet.remove("");
}

public
boolean accept(File file)
{
final
Iterator suffices = suffixSet.iterator();

while (suffices.hasNext())
{
if (file.getName().toLowerCase().endsWith(suffices.ne xt().toString()))
{
return
true;
}
}

return
false;
}
}
--
"I love Saturday morning cartoons, what classic humour! This is what
entertainment is all about ... Idiots, explosives and falling anvils."
-- Calvin and Hobbes, Bill Watterson

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Gtk::FileFilter WoodHacker Ruby 2 11-02-2006 12:05 AM
Wildcard in FileFilter is resetting my File Type cjaouhar@hotmail.com Java 0 05-26-2006 03:14 PM
Trouble extending java.io.FileFilter! Frank Java 5 01-14-2005 08:54 AM
FileList and FileFilter and regular expressions P.Hill Java 6 11-18-2003 03:15 PM



Advertisments