KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > incava > io > Find


1 package org.incava.io;
2
3 import java.io.*;
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6
7
8 /**
9  * Support for <code>find(1)</code>-like behavior.
10  */

11 public class Find
12 {
13     /**
14      * Passes back a list of files. Directories are processed recursively,
15      * collecting files with the suffix of <code>suffix</code>. If
16      * <code>name</code> refers to a file, it is simply added to the list.
17      */

18     public static void getFileList(List JavaDoc fileList, String JavaDoc name, final String JavaDoc suffix)
19     {
20         try {
21             File fd = new File(name);
22             if (fd.isDirectory()) {
23                 tr.Ace.log("processing directory");
24                 String JavaDoc[] contents = fd.list(new FilenameFilter() {
25                         public boolean accept(File dir, String JavaDoc nm) {
26                             File f = new File(dir, nm);
27                             return f.isDirectory() || (f.isFile() && nm.endsWith(suffix));
28                         }
29                     });
30                 for (int ci = 0; contents != null && ci < contents.length; ++ci) {
31                     getFileList(fileList, name + File.separator + contents[ci], suffix);
32                 }
33             }
34             else if (fd.isFile()) {
35                 tr.Ace.log("adding: " + fd);
36                 fileList.add(fd.getCanonicalPath());
37             }
38             else {
39                 System.err.println(name + " not found.");
40             }
41         }
42         catch (FileNotFoundException e) {
43             System.err.println("File " + name + " not found.");
44         }
45         catch (IOException e) {
46             System.err.println("Error opening " + name + ": " + e);
47         }
48     }
49
50     /**
51      * Returns an array of files, collected from the <code>names</code> list.
52      * Directories are processed recursively, collecting files with
53      * <code>suffix</code>. If <code>name</code> refers to a file, it is simply
54      * added to the list.
55      */

56     public static String JavaDoc[] getFileList(String JavaDoc[] names, String JavaDoc suffix)
57     {
58         List JavaDoc fileList = new ArrayList JavaDoc();
59         for (int i = 0; i < names.length; ++i) {
60             getFileList(fileList, names[i], suffix);
61         }
62         String JavaDoc[] fileNames = (String JavaDoc[])fileList.toArray(new String JavaDoc[0]);
63         return fileNames;
64     }
65
66 }
67
Popular Tags