Results 1 to 8 of 8
  1. #1
    Mandingo is offline Member
    Join Date
    Aug 2013
    Posts
    7
    Rep Power
    0

    Default Filter for Files or Directorys

    Hello world =P
    This is a filter class that was made to return .java Files or files that end with "s" - that worked well.
    Now I want to change it, so that it returns true for files or directories which does not work.

    Java Code:
    import java.io.File;
    import java.io.FilenameFilter;
    public class FilterType implements FilenameFilter{
    //this gives path name and filename - but as a String
      public boolean accept(File f, String s){
        //return s.toLowerCase().endsWith("s");
        //return s.toLowerCase().endsWith("java");
        File fl;
    //the file must be reconstructed - I don't know if "getAbsoluteFile();" can do this
        fl=f.getAbsoluteFile();
    //to be fed to .isFile() / .isDirectory()
        return fl.isDirectory();
      }
    }
    It is called by this program that lists a directorys content, filtered calling FilterType:
    Java Code:
    import java.io.*;
    public class TestFilenameFilter{
      public static void main(String[] args){
        File dir=new File(".");
        String[] liste=dir.list(new FilterType());
        for (int i=0; i<liste.length; i++) 
        System.out.println(liste[i]);
      }
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,357
    Rep Power
    6

    Default Re: Filter for Files or Directorys

    // the file must be reconstructed - I don't know if "getAbsoluteFile();" can do this
    Well what does the File javadoc say? No need to guess, read the documentation and KNOW what is the truth.

    Google: java 7 File
    result: File (Java Platform SE 7 )

    Perhaps there are some other methods in there that fit more to what you need.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Filter for Files or Directorys

    Why not put a print statement in your accept method to see what is being passed as arguments?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Mandingo is offline Member
    Join Date
    Aug 2013
    Posts
    7
    Rep Power
    0

    Default Re: Filter for Files or Directorys

    Java Code:
    System.out.println("f: "+f+" s: "+s);
    This would be nice, but it does not work in the accept method.

    f I think is the path, equally to something like getParent() or getParentFile() would give. s is a String with the file name in it, for example test.txt. But it's a string, so it is not possible to use isFile() on it!

    File (Java Platform SE 7 ) I've tried other things in there, too.

    I'm near to cutting my arms open because of this one. Don't give me tips you don't know either =)
    Last edited by Mandingo; 08-05-2013 at 09:47 PM.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Filter for Files or Directorys

    What do you mean it doesn't work? Did you try it?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,357
    Rep Power
    6

    Default Re: Filter for Files or Directorys

    Quote Originally Posted by Mandingo View Post
    f I think is the path, equally to something like getParent() or getParentFile() would give. s is a String with the file name in it, for example test.txt. But it's a string, so it is not possible to use isFile() on it!
    So perhaps you need to create a File so you can use isFile() (or isDirectory()) on it. Take a look at the constructors of the File class - isn't there a convenient one in there that does exactly what you need?

    But perhaps there is another way to go about this entirely. See, you're using a filename filter. But you don't want to filter on filenames anymore do you? You want to filter on files. Perhaps there is a class that may work better in this case? Again: check out the File class for the clue, it has more list methods than the one you're using.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Mandingo is offline Member
    Join Date
    Aug 2013
    Posts
    7
    Rep Power
    0

    Default Re: Filter for Files or Directorys

    Great - this works!!!

    Java Code:
    import java.io.*;
    public class TestFF{
      public static void main(String[] args){
        File dir=new File(".");
        File[] liste=dir.listFiles(new FileFilterFF());
        for (int i=0; i<liste.length; i++) 
        System.out.println(liste[i]);
      }
    }
    Java Code:
    import java.io.File;
    import java.io.FileFilter;
    public class FileFilterFF implements FileFilter{
      public boolean accept(File f){
        return f.isFile();
        //return f.isDirectory();
      }
    }

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,357
    Rep Power
    6

    Default Re: Filter for Files or Directorys

    Good job! But you also wanted to allow directories, so you're ALMOST there ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. How to set Document Filter
    By nimishalex in forum New To Java
    Replies: 2
    Last Post: 12-27-2010, 02:08 PM
  2. web content filter or internet filter
    By sundarjothi in forum Advanced Java
    Replies: 3
    Last Post: 05-15-2008, 12:36 PM
  3. How to filter files in file upload using html contorl
    By deivaganesh in forum Advanced Java
    Replies: 0
    Last Post: 01-29-2008, 07:31 AM
  4. Listing subdirectories/files with filter
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:20 AM
  5. Need a filter
    By revathi17 in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 08:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •