Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > FilenameFilter details

Reply
Thread Tools

FilenameFilter details

 
 
Bill Medland
Guest
Posts: n/a
 
      03-02-2006
I am just working through an example in Bruce Eckel's "Thinking in
Java" (3rd Edn), looking at his first Directory Listing example. I
don't see why his FilenameFilter is as complex as it is and so I assume
that there is something I am not understanding about FilenameFilter.
Unfortunately the 1.4.2 documentation isn't any clearer. Can someone
explain what the danger is that Bruce is avoiding?

Bruce's DirFilter has the following accept method;
public boolean accept (File dir, String name) {
// Strip path information, search for regex:
return pattern.matcher(
new File(name).getName()).matches();
}

Why is it not simply return pattern.matcher(name).matches?

The comment seems to suggest that 'name' might be a full path but I see
that it isn't when I run it on my machine.
The 1.4.2 documentation seems to suggest that 'name' is the actual file
name within 'dir'

So when would new File(name).getName() not be name?

Or is it just bulletproofing the code?

 
Reply With Quote
 
 
 
 
Roland de Ruiter
Guest
Posts: n/a
 
      03-03-2006
Bill Medland wrote:
> I am just working through an example in Bruce Eckel's "Thinking in
> Java" (3rd Edn), looking at his first Directory Listing example. I
> don't see why his FilenameFilter is as complex as it is and so I assume
> that there is something I am not understanding about FilenameFilter.
> Unfortunately the 1.4.2 documentation isn't any clearer. Can someone
> explain what the danger is that Bruce is avoiding?
>
> Bruce's DirFilter has the following accept method;
> public boolean accept (File dir, String name) {
> // Strip path information, search for regex:
> return pattern.matcher(
> new File(name).getName()).matches();
> }
>
> Why is it not simply return pattern.matcher(name).matches?
>
> The comment seems to suggest that 'name' might be a full path but I see
> that it isn't when I run it on my machine.
> The 1.4.2 documentation seems to suggest that 'name' is the actual file
> name within 'dir'
>
> So when would new File(name).getName() not be name?
>
> Or is it just bulletproofing the code?
>


I agree with you: Bruce Eckel's code is confusing and unnecessary. The
expression
pattern.matcher(name).matches();
should be sufficient.

Maybe Mr. Eckel was thinking of a FileFilter rather than a
FilenameFilter. The FileFilter's accept method does take a single
argument, a File, which contains the full path of the file (including
its name). In this case, the directory part must be stripped, to obtain
the name. The code would then like this

class DirFileFilter implements FileFilter {
private Pattern pattern = ...
public boolean accept(File file) {
return pattern.matcher(file.getName()).matches();
}
}

In the FilenameFilter accept method, however, 'name' is the name of the
actual file in the 'dir', just like you and the documentation suggest.

Regards,

Roland
 
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
FilenameFilter woes Alan Java 10 12-29-2007 02:21 PM
how can i modify this program to list the *.java file using FIlenameFilter mahesh Java 3 12-06-2006 11:06 AM
how can i modify this program to list the *.java file using FIlenameFilter mahesh Java 0 12-06-2006 05:14 AM
Show Details/Hide Details link button =?Utf-8?B?Sm9l?= ASP .Net 1 03-13-2006 04:51 PM
Re: Easy FileFiletr/FilenameFilter Andy Flowers Java 0 06-29-2003 08:54 AM



Advertisments