FilenameFilter details

Discussion in 'Java' started by Bill Medland, Mar 2, 2006.

  1. Bill Medland

    Bill Medland Guest

    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?
    Bill Medland, Mar 2, 2006
    #1
    1. Advertising

  2. 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
    Roland de Ruiter, Mar 3, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    Show Details/Hide Details link button

    =?Utf-8?B?Sm9l?=, Mar 13, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    895
    dkode
    Mar 13, 2006
  2. Andy Flowers

    Re: Easy FileFiletr/FilenameFilter

    Andy Flowers, Jun 29, 2003, in forum: Java
    Replies:
    0
    Views:
    852
    Andy Flowers
    Jun 29, 2003
  3. mahesh
    Replies:
    3
    Views:
    360
    GenxLogic
    Dec 6, 2006
  4. mahesh
    Replies:
    0
    Views:
    330
    mahesh
    Dec 6, 2006
  5. Alan

    FilenameFilter woes

    Alan, Dec 29, 2007, in forum: Java
    Replies:
    10
    Views:
    722
    Andrew Thompson
    Dec 29, 2007
Loading...

Share This Page