Dir filter how to set ?

Discussion in 'Java' started by moonhk, Aug 17, 2006.

  1. moonhk

    moonhk Guest

    Now. Output as below, I want just list out *.java , how to using filter

    ?

    1 i = 60, 2003-01-22 23:01:02, .\testing\template.java
    1 i = 61, 2003-01-12 01:38:02, .\testing\Triangle.class
    1 i = 62, 2003-01-21 22:32:28, .\testing\user.home
    1 i = 63, 2003-01-17 20:27:54, .\testing\Vehicle.class
    0 i = 100, 2006-08-12 00:58:44, .\testing.class
    0 i = 101, 2005-07-31 00:27:58, .\testing.java
    0 i = 102, 2005-07-22 08:50:19, .\Thread
    1 i = 0, 2005-07-21 22:59:16, .\Thread\MaskingThread.class
    1 i = 1, 2003-02-14 12:01:16, .\Thread\MaskingThread.java
    1 i = 2, 2003-03-01 23:18:20, .\Thread\ThreadDemo.java
    1 i = 3, 2005-10-04 10:48:10, .\Thread\WithThread.java
    0 i = 103, 2005-08-04 10:42:30, .\unicode


    import java.io.*;
    import java.util.*;
    import java.text.*; // For DateFormat


    public class Dir {
    static int indentLevel = - 1;
    Dir (String path) {
    listPath (new File (path));
    }


    void listPath (File path) {
    File files []; //List of files in a Directory
    indentLevel++; // Going Down...


    // Create list of files in this dir
    files = path.listFiles();


    // Sort with help of Collections API
    Arrays.sort(files);
    for (int i= 0, n=files.length ; i < n ; i++) {
    for (int indent = 0 ; indent <
    indentLevel; indent++) {
    System.out.print(" ");
    }
    // DateFormat df = new SimpleDateFormat("yyyy-MM-dd
    HH:mm:ss.SSS");
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd
    HH:mm:ss");
    Date date = new Date(files.lastModified());
    //String s =
    df.getDateInstance(DateFormat.MEDIUM).format(date);
    String s = df.format(date);


    System.out.println(indentLevel + " i = " + i + ", " +
    s + ", " +
    files.toString());


    if (files.isDirectory()) {
    listPath(files);
    }
    }
    indentLevel--; // and going up
    }
    // Main
    public static void main (String args[]) {
    new Dir(args[0]);


    }
    }


    Reply »
     
    moonhk, Aug 17, 2006
    #1
    1. Advertising

  2. On 17-8-2006 8:50, moonhk wrote:
    > Now. Output as below, I want just list out *.java , how to using filter
    >
    > ?
    >
    > 1 i = 60, 2003-01-22 23:01:02, .\testing\template.java
    > 1 i = 61, 2003-01-12 01:38:02, .\testing\Triangle.class
    > 1 i = 62, 2003-01-21 22:32:28, .\testing\user.home
    > 1 i = 63, 2003-01-17 20:27:54, .\testing\Vehicle.class
    > 0 i = 100, 2006-08-12 00:58:44, .\testing.class
    > 0 i = 101, 2005-07-31 00:27:58, .\testing.java
    > 0 i = 102, 2005-07-22 08:50:19, .\Thread
    > 1 i = 0, 2005-07-21 22:59:16, .\Thread\MaskingThread.class
    > 1 i = 1, 2003-02-14 12:01:16, .\Thread\MaskingThread.java
    > 1 i = 2, 2003-03-01 23:18:20, .\Thread\ThreadDemo.java
    > 1 i = 3, 2005-10-04 10:48:10, .\Thread\WithThread.java
    > 0 i = 103, 2005-08-04 10:42:30, .\unicode
    >
    >
    > import java.io.*;
    > import java.util.*;
    > import java.text.*; // For DateFormat
    >
    >
    > public class Dir {
    > static int indentLevel = - 1;
    > Dir (String path) {
    > listPath (new File (path));
    > }
    >
    >
    > void listPath (File path) {
    > File files []; //List of files in a Directory
    > indentLevel++; // Going Down...
    >
    >
    > // Create list of files in this dir
    > files = path.listFiles();
    >
    >
    > // Sort with help of Collections API
    > Arrays.sort(files);
    > for (int i= 0, n=files.length ; i < n ; i++) {
    > for (int indent = 0 ; indent <
    > indentLevel; indent++) {
    > System.out.print(" ");
    > }
    > // DateFormat df = new SimpleDateFormat("yyyy-MM-dd
    > HH:mm:ss.SSS");
    > DateFormat df = new SimpleDateFormat("yyyy-MM-dd
    > HH:mm:ss");
    > Date date = new Date(files.lastModified());
    > //String s =
    > df.getDateInstance(DateFormat.MEDIUM).format(date);
    > String s = df.format(date);
    >
    >
    > System.out.println(indentLevel + " i = " + i + ", " +
    > s + ", " +
    > files.toString());
    >
    >
    > if (files.isDirectory()) {
    > listPath(files);
    > }
    > }
    > indentLevel--; // and going up
    > }
    > // Main
    > public static void main (String args[]) {
    > new Dir(args[0]);
    >
    >
    > }
    > }
    >
    >
    > Reply »
    >


    This looks like a homework question. Start reading the following:
    <http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#listFiles(java.io.FilenameFilter)>
    <http://java.sun.com/j2se/1.5.0/docs/api/java/io/FilenameFilter.html>
    <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#endsWith(java.lang.String)>
    If you have specific questions, just ask again.
    --
    Regards,

    Roland
     
    Roland de Ruiter, Aug 17, 2006
    #2
    1. Advertising

  3. moonhk

    moonhk Guest

    Roland de Ruiter wrote:
    > On 17-8-2006 8:50, moonhk wrote:
    > > Now. Output as below, I want just list out *.java , how to using filter
    > >
    > > ?
    > >
    > > 1 i = 60, 2003-01-22 23:01:02, .\testing\template.java
    > > 1 i = 61, 2003-01-12 01:38:02, .\testing\Triangle.class
    > > 1 i = 62, 2003-01-21 22:32:28, .\testing\user.home
    > > 1 i = 63, 2003-01-17 20:27:54, .\testing\Vehicle.class
    > > 0 i = 100, 2006-08-12 00:58:44, .\testing.class
    > > 0 i = 101, 2005-07-31 00:27:58, .\testing.java
    > > 0 i = 102, 2005-07-22 08:50:19, .\Thread
    > > 1 i = 0, 2005-07-21 22:59:16, .\Thread\MaskingThread.class
    > > 1 i = 1, 2003-02-14 12:01:16, .\Thread\MaskingThread.java
    > > 1 i = 2, 2003-03-01 23:18:20, .\Thread\ThreadDemo.java
    > > 1 i = 3, 2005-10-04 10:48:10, .\Thread\WithThread.java
    > > 0 i = 103, 2005-08-04 10:42:30, .\unicode
    > >
    > >
    > > import java.io.*;
    > > import java.util.*;
    > > import java.text.*; // For DateFormat
    > >
    > >
    > > public class Dir {
    > > static int indentLevel = - 1;
    > > Dir (String path) {
    > > listPath (new File (path));
    > > }
    > >
    > >
    > > void listPath (File path) {
    > > File files []; //List of files in a Directory
    > > indentLevel++; // Going Down...
    > >
    > >
    > > // Create list of files in this dir
    > > files = path.listFiles();
    > >
    > >
    > > // Sort with help of Collections API
    > > Arrays.sort(files);
    > > for (int i= 0, n=files.length ; i < n ; i++) {
    > > for (int indent = 0 ; indent <
    > > indentLevel; indent++) {
    > > System.out.print(" ");
    > > }
    > > // DateFormat df = new SimpleDateFormat("yyyy-MM-dd
    > > HH:mm:ss.SSS");
    > > DateFormat df = new SimpleDateFormat("yyyy-MM-dd
    > > HH:mm:ss");
    > > Date date = new Date(files.lastModified());
    > > //String s =
    > > df.getDateInstance(DateFormat.MEDIUM).format(date);
    > > String s = df.format(date);
    > >
    > >
    > > System.out.println(indentLevel + " i = " + i + ", " +
    > > s + ", " +
    > > files.toString());
    > >
    > >
    > > if (files.isDirectory()) {
    > > listPath(files);
    > > }
    > > }
    > > indentLevel--; // and going up
    > > }
    > > // Main
    > > public static void main (String args[]) {
    > > new Dir(args[0]);
    > >
    > >
    > > }
    > > }
    > >
    > >
    > > Reply »
    > >

    >
    > This looks like a homework question. Start reading the following:
    > <http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#listFiles(java..io.FilenameFilter)>
    > <http://java.sun.com/j2se/1.5.0/docs/api/java/io/FilenameFilter.html>
    > <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#endsWith(java.lang.String)>
    > If you have specific questions, just ask again.
    > --
    > Regards,



    >
    > Roland


    Thank. I already read the those documents before, but still can not
    able to apply filter.
    Yes. I am testing Java function.

    Try to delete files if last Modified less than some value.
     
    moonhk, Aug 17, 2006
    #3
  4. On 17-8-2006 11:37, moonhk wrote:
    > Roland de Ruiter wrote:
    >> On 17-8-2006 8:50, moonhk wrote:
    >>> Now. Output as below, I want just list out *.java , how to using filter
    >>>
    >>> ?
    >>>
    >>> 1 i = 60, 2003-01-22 23:01:02, .\testing\template.java
    >>> 1 i = 61, 2003-01-12 01:38:02, .\testing\Triangle.class
    >>> 1 i = 62, 2003-01-21 22:32:28, .\testing\user.home
    >>> 1 i = 63, 2003-01-17 20:27:54, .\testing\Vehicle.class
    >>> 0 i = 100, 2006-08-12 00:58:44, .\testing.class
    >>> 0 i = 101, 2005-07-31 00:27:58, .\testing.java
    >>> 0 i = 102, 2005-07-22 08:50:19, .\Thread
    >>> 1 i = 0, 2005-07-21 22:59:16, .\Thread\MaskingThread.class
    >>> 1 i = 1, 2003-02-14 12:01:16, .\Thread\MaskingThread.java
    >>> 1 i = 2, 2003-03-01 23:18:20, .\Thread\ThreadDemo.java
    >>> 1 i = 3, 2005-10-04 10:48:10, .\Thread\WithThread.java
    >>> 0 i = 103, 2005-08-04 10:42:30, .\unicode
    >>>
    >>>
    >>> import java.io.*;
    >>> import java.util.*;
    >>> import java.text.*; // For DateFormat
    >>>
    >>>
    >>> public class Dir {
    >>> static int indentLevel = - 1;
    >>> Dir (String path) {
    >>> listPath (new File (path));
    >>> }
    >>>
    >>>
    >>> void listPath (File path) {
    >>> File files []; //List of files in a Directory
    >>> indentLevel++; // Going Down...
    >>>
    >>>
    >>> // Create list of files in this dir
    >>> files = path.listFiles();
    >>>
    >>>
    >>> // Sort with help of Collections API
    >>> Arrays.sort(files);
    >>> for (int i= 0, n=files.length ; i < n ; i++) {
    >>> for (int indent = 0 ; indent <
    >>> indentLevel; indent++) {
    >>> System.out.print(" ");
    >>> }
    >>> // DateFormat df = new SimpleDateFormat("yyyy-MM-dd
    >>> HH:mm:ss.SSS");
    >>> DateFormat df = new SimpleDateFormat("yyyy-MM-dd
    >>> HH:mm:ss");
    >>> Date date = new Date(files.lastModified());
    >>> //String s =
    >>> df.getDateInstance(DateFormat.MEDIUM).format(date);
    >>> String s = df.format(date);
    >>>
    >>>
    >>> System.out.println(indentLevel + " i = " + i + ", " +
    >>> s + ", " +
    >>> files.toString());
    >>>
    >>>
    >>> if (files.isDirectory()) {
    >>> listPath(files);
    >>> }
    >>> }
    >>> indentLevel--; // and going up
    >>> }
    >>> // Main
    >>> public static void main (String args[]) {
    >>> new Dir(args[0]);
    >>>
    >>>
    >>> }
    >>> }
    >>>
    >>>
    >>> Reply »
    >>>

    >> This looks like a homework question. Start reading the following:
    >> <http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#listFiles(java.io.FilenameFilter)>
    >> <http://java.sun.com/j2se/1.5.0/docs/api/java/io/FilenameFilter.html>
    >> <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#endsWith(java.lang.String)>
    >> If you have specific questions, just ask again.
    >> --
    >> Regards,

    >
    >
    >> Roland

    >
    > Thank. I already read the those documents before, but still can not
    > able to apply filter.
    > Yes. I am testing Java function.
    >
    > Try to delete files if last Modified less than some value.
    >

    What do you mean by "not able to apply filter"?
    --
    Regards,

    Roland
     
    Roland de Ruiter, Aug 17, 2006
    #4
  5. moonhk

    moonhk Guest

    I try to user filter , but wrong format. Now changed to

    final String mchFn = fn ;
    FilenameFilter filter = new FilenameFilter() {
    public boolean accept(File dir, String name) {
    return name.endsWith(mchFn);
    }
    };

    // Create list of files in this dir
    files = path.listFiles(filter);

    But, Just able to list File in current directory. Due to filter just
    list of *.java file not include directory.
    Next step, try to include directory.


    C:\Example\javaux>java Dir ./ java
    0 i = 0, 2006-07-07 14:03:05, .\conn2.java
    0 i = 1, 2006-07-07 14:39:28, .\conn_rtitem.java
    0 i = 2, 2006-07-07 14:03:05, .\conn_unix.java
    0 i = 3, 2006-07-07 14:03:06, .\conn_user.java
    0 i = 4, 2005-07-29 23:19:54, .\decode_mrp.java
    0 i = 5, 2006-08-18 10:31:09, .\Dir.java
    0 i = 6, 2006-08-11 12:24:29, .\dir2.java
    0 i = 7, 2006-08-17 13:43:17, .\Dir_t.java
    0 i = 8, 2003-02-13 00:17:28, .\elapsedTime.java
    0 i = 9, 2003-02-04 20:05:32, .\ex_date03.java
    0 i = 10, 2003-02-19 21:55:32, .\function.java
    0 i = 11, 2004-03-17 16:55:02, .\HELLO.java
    0 i = 12, 2003-02-14 12:01:16, .\MaskingThread.java
    0 i = 13, 2003-02-14 12:02:54, .\PasswordApp.java
    0 i = 14, 2003-02-14 12:02:18, .\PasswordField.java
    0 i = 15, 2003-01-14 14:36:44, .\printx.java
    0 i = 16, 2003-02-14 11:39:36, .\string_taken.java
    0 i = 17, 2005-08-03 21:55:08, .\template.java
    0 i = 18, 2005-08-03 22:01:24, .\template2.java
    0 i = 19, 2005-08-03 22:19:04, .\test_8bit.java
    0 i = 20, 2005-07-31 00:30:10, .\test_cast.java
    0 i = 21, 2005-07-29 00:29:40, .\test_double.java
    0 i = 22, 2005-07-31 00:38:26, .\test_read.java
    0 i = 23, 2005-07-29 10:59:55, .\test_string.java
    0 i = 24, 2005-07-31 00:26:26, .\test_while.java
    0 i = 25, 2005-07-31 00:27:58, .\testing.java
    java

    C:\Example\javaux>

    /* Application
    2006/08/18 eric.leung App filter
    2006/08/17 eric.leung
    2005/07/21 eric.leung OK

    java Dir .\ java

    Output
    =======
    0 i = 100 2006-08-12 00:58:44 .\testing.class
    0 i = 101 2005-07-31 00:27:58 .\testing.java
    0 i = 102 2005-07-22 08:50:19 .\Thread
    1 i = 0 2005-07-21 22:59:16 .\Thread\MaskingThread.class
    1 i = 1 2003-02-14 12:01:16 .\Thread\MaskingThread.java
    1 i = 2 2003-03-01 23:18:20 .\Thread\ThreadDemo.java
    1 i = 3 2005-10-04 10:48:10 .\Thread\WithThread.java


    */


    import java.io.*;
    import java.util.*;
    import java.text.*; // For DateFormat

    public class Dir {
    static int indentLevel = - 1;
    Dir (String path,String fn) {
    listPath (new File (path), fn);
    }

    void listPath (File path,String fn) {
    File files []; //List of files in a Directory
    indentLevel++; // Going Down...

    // http://javaalmanac.com/egs/java.io/GetFiles.html
    // It is also possible to filter the list of returned files.
    // This example does not return any files that start with `.'.

    final String mchFn = fn ;
    FilenameFilter filter = new FilenameFilter() {
    public boolean accept(File dir, String name) {
    return name.endsWith(mchFn);
    }
    };

    // Create list of files in this dir
    files = path.listFiles(filter);

    // Sort with help of Collections API
    Arrays.sort(files);
    for (int i= 0, n=files.length ; i < n ; i++) {
    for (int indent = 0 ; indent < indentLevel; indent++) {

    System.out.print(" ");
    }
    // DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date(files.lastModified());
    //String s = df.getDateInstance(DateFormat.MEDIUM).format(date);
    String s = df.format(date);

    System.out.println(indentLevel + " i = " + i + ", " + s + ", " +
    files.toString());



    if (files.isDirectory()) {
    listPath(files,fn);
    }
    }
    indentLevel--; // and going up
    }
    // Main
    public static void main (String args[]) {
    new Dir(args[0],args[1]);
    System.out.println(args[1]);

    }
    }
     
    moonhk, Aug 18, 2006
    #5
    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. =?Utf-8?B?UnVkeQ==?=

    Sub Dir, Virtual dir, what do I use?

    =?Utf-8?B?UnVkeQ==?=, Jun 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    444
    =?Utf-8?B?UnVkeQ==?=
    Jun 12, 2005
  2. Matthew Denner
    Replies:
    1
    Views:
    212
  3. Kga Agk
    Replies:
    2
    Views:
    160
    Kga Agk
    Jun 29, 2009
  4. Nick Gnedin
    Replies:
    2
    Views:
    175
  5. MRAB
    Replies:
    0
    Views:
    134
Loading...

Share This Page