Dir filter how to set ?

M

moonhk

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 »
 
R

Roland de Ruiter

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

moonhk

Roland said:
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.
 
R

Roland de Ruiter

Roland said:
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.


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"?
 
M

moonhk

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]);

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top