how FilenameFilter works?

Discussion in 'Java' started by shyam_oec, Feb 16, 2008.

  1. shyam_oec

    shyam_oec New Member

    Joined:
    Nov 26, 2007
    Messages:
    89
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Developer, .NET Framework
    Location:
    Jamshedpur
    With reference to the code below can u tell me when and how accept() method of FilenameFilter is called,and from where parameter of accept() method are passed?
    In a nutshell i mean to say how FilenameFilter works?

    Code:
    import java.io.*;
    
    pubic class OnlyExt implements FilenameFilter
    {
      String str;
     
       public OnlyExt(String str)
       {
            this.str="."+str;
        }
    
     public boolean accept(File dir,String name)
       {
           return name.endsWith(str);
       }
    }
    
    class DirListOnly
    {
       public static void main(String [] args)
       {
          String dirname="/java";
           File f1=new File(dirname);
           FilenameFilter only=new OnlyExt("html");
           String s[]=f1.list(only);
    
        for(int i=0;i<s.length;i++)
        {
           System.out.println(s[i]);
        }
     }
    }
     
    Last edited by a moderator: Feb 16, 2008

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice