Re: FileFilter

Discussion in 'Java' started by Sebastian Hoehn, Jun 29, 2003.

  1. 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
    Sebastian Hoehn, Jun 29, 2003
    #1
    1. Advertising

  2. Sebastian Hoehn

    Bhun Guest

    "Sebastian Hoehn" <>, 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(".xml");
    }
    }
    ...
    }

    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.next().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
    Bhun, Jun 29, 2003
    #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. P.Hill
    Replies:
    6
    Views:
    6,302
    P.Hill
    Nov 18, 2003
  2. Frank
    Replies:
    5
    Views:
    495
    Andrew Thompson
    Jan 14, 2005
  3. Replies:
    0
    Views:
    399
  4. WoodHacker

    Gtk::FileFilter

    WoodHacker, Nov 1, 2006, in forum: Ruby
    Replies:
    2
    Views:
    149
    WoodHacker
    Nov 2, 2006
Loading...

Share This Page