Java Forum / General / August 2006
Dir filter how to set ?
moonhk - 17 Aug 2006 07:50 GMT 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[i].lastModified()); //String s = df.getDateInstance(DateFormat.MEDIUM).format(date); String s = df.format(date);
System.out.println(indentLevel + " i = " + i + ", " + s + ", " + files[i].toString());
if (files[i].isDirectory()) { listPath(files[i]); } } indentLevel--; // and going up } // Main public static void main (String args[]) { new Dir(args[0]);
} }
Reply »
Roland de Ruiter - 17 Aug 2006 09:40 GMT > Now. Output as below, I want just list out *.java , how to using filter > [quoted text clipped - 64 lines] > > 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.File nameFilter)> <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.
 Signature Regards,
Roland
moonhk - 17 Aug 2006 10:37 GMT > > Now. Output as below, I want just list out *.java , how to using filter > > [quoted text clipped - 72 lines] > -- > 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.
Roland de Ruiter - 17 Aug 2006 13:31 GMT >>> Now. Output as below, I want just list out *.java , how to using filter >>> [quoted text clipped - 80 lines] > > Try to delete files if last Modified less than some value. What do you mean by "not able to apply filter"?
 Signature Regards,
Roland
moonhk - 18 Aug 2006 03:39 GMT 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[i].lastModified()); //String s = df.getDateInstance(DateFormat.MEDIUM).format(date); String s = df.format(date);
System.out.println(indentLevel + " i = " + i + ", " + s + ", " + files[i].toString());
if (files[i].isDirectory()) { listPath(files[i],fn); } } indentLevel--; // and going up } // Main public static void main (String args[]) { new Dir(args[0],args[1]); System.out.println(args[1]); } }
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|