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. Advertisements

  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. Advertisements

  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. Advertisements

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. Bill Medland

    FilenameFilter details

    Bill Medland, Mar 2, 2006, in forum: Java
    Replies:
    1
    Views:
    2,600
    Roland de Ruiter
    Mar 3, 2006
  2. Venkat
    Replies:
    12
    Views:
    11,066
    Chris \( Val \)
    Jan 8, 2004
  3. mahesh
    Replies:
    0
    Views:
    411
    mahesh
    Dec 6, 2006
  4. Shawn
    Replies:
    2
    Views:
    1,874
    Shawn
    Dec 6, 2006
  5. Alan

    FilenameFilter woes

    Alan, Dec 29, 2007, in forum: Java
    Replies:
    10
    Views:
    971
    Andrew Thompson
    Dec 29, 2007
  6. Matrixinline
    Replies:
    2
    Views:
    640
    Nobody
    Sep 21, 2009
  7. Lin Jingxian
    Replies:
    2
    Views:
    166
    Tad McClellan
    May 9, 2005
  8. Roedy Green
    Replies:
    4
    Views:
    349
    Andreas Leitgeb
    Mar 22, 2014
Loading...