FilenameFilter details

B

Bill Medland

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?
 
R

Roland de Ruiter

Bill said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top