how can i modify this program to list the *.java file using FIlenameFilter

Discussion in 'Java' started by mahesh, Dec 6, 2006.

  1. mahesh

    mahesh Guest

    public class MatcherEx //implements FilenameFilter
    {
    static String suffix=".txt";
    private FilenameFilter filter;

    public static void main(String[] args)throws IOException {

    File fs=new File("\\d:");
    File []path1=fs.listFiles();

    for(int i=0;i<path1.length;i++)
    {
    System.out.println("\n"+path1.toString());
    }
    }
    }
    mahesh, Dec 6, 2006
    #1
    1. Advertising

  2. mahesh wrote:

    Sub: how can i modify this program to list the *.java file using
    FIlenameFilter

    Try using Java code.

    Andrew T.
    Andrew Thompson, Dec 6, 2006
    #2
    1. Advertising

  3. mahesh wrote:
    > public class MatcherEx //implements FilenameFilter
    > {
    > static String suffix=".txt";
    > private FilenameFilter filter;
    >
    > public static void main(String[] args)throws IOException {
    >
    > File fs=new File("\\d:");
    > File []path1=fs.listFiles();
    >
    > for(int i=0;i<path1.length;i++)
    > {
    > System.out.println("\n"+path1.toString());
    > }
    > }
    > }
    >


    Here you go -

    import java.io.*;

    public class test implements FilenameFilter {
    public test() {
    File fs = new File("c:");
    String[] paths = fs.list(this);
    for (int i=0; i<paths.length; i++)
    System.out.println(paths);
    }

    public boolean accept(File dir, String name) {
    if (name.endsWith(".java"))
    return true;
    else
    return false;
    }

    public static void main(String[] args) {
    new test();
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Dec 6, 2006
    #3
  4. mahesh

    GenxLogic Guest

    If you are using FileNameFilter then you have to make your own
    FileNameFilter which will implement FileNameFilter Interface and will
    implement the public boolean accept(File file,String name) function.
    this function will conatin the logic you want to apply for the
    selection of the files.

    mahesh wrote:
    > public class MatcherEx //implements FilenameFilter
    > {
    > static String suffix=".txt";
    > private FilenameFilter filter;
    >
    > public static void main(String[] args)throws IOException {
    >
    > File fs=new File("\\d:");
    > File []path1=fs.listFiles();
    >
    > for(int i=0;i<path1.length;i++)
    > {
    > System.out.println("\n"+path1.toString());
    > }
    > }
    > }
    GenxLogic, Dec 6, 2006
    #4
    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. Andy Flowers

    Re: Easy FileFiletr/FilenameFilter

    Andy Flowers, Jun 29, 2003, in forum: Java
    Replies:
    0
    Views:
    854
    Andy Flowers
    Jun 29, 2003
  2. Bill Medland

    FilenameFilter details

    Bill Medland, Mar 2, 2006, in forum: Java
    Replies:
    1
    Views:
    2,439
    Roland de Ruiter
    Mar 3, 2006
  3. mahesh
    Replies:
    0
    Views:
    331
    mahesh
    Dec 6, 2006
  4. Alan

    FilenameFilter woes

    Alan, Dec 29, 2007, in forum: Java
    Replies:
    10
    Views:
    724
    Andrew Thompson
    Dec 29, 2007
  5. Lin Jingxian
    Replies:
    2
    Views:
    87
    Tad McClellan
    May 9, 2005
Loading...

Share This Page