01: package org.incava.io;
02:
03: import java.io.*;
04: import java.util.ArrayList;
05: import java.util.List;
06:
07: /**
08: * Support for <code>find(1)</code>-like behavior.
09: */
10: public class Find {
11: /**
12: * Passes back a list of files. Directories are processed recursively,
13: * collecting files with the suffix of <code>suffix</code>. If
14: * <code>name</code> refers to a file, it is simply added to the list.
15: */
16: public static void getFileList(List fileList, String name,
17: final String suffix) {
18: try {
19: File fd = new File(name);
20: if (fd.isDirectory()) {
21: tr.Ace.log("processing directory");
22: String[] contents = fd.list(new FilenameFilter() {
23: public boolean accept(File dir, String nm) {
24: File f = new File(dir, nm);
25: return f.isDirectory()
26: || (f.isFile() && nm.endsWith(suffix));
27: }
28: });
29: for (int ci = 0; contents != null
30: && ci < contents.length; ++ci) {
31: getFileList(fileList, name + File.separator
32: + contents[ci], suffix);
33: }
34: } else if (fd.isFile()) {
35: tr.Ace.log("adding: " + fd);
36: fileList.add(fd.getCanonicalPath());
37: } else {
38: System.err.println(name + " not found.");
39: }
40: } catch (FileNotFoundException e) {
41: System.err.println("File " + name + " not found.");
42: } catch (IOException e) {
43: System.err.println("Error opening " + name + ": " + e);
44: }
45: }
46:
47: /**
48: * Returns an array of files, collected from the <code>names</code> list.
49: * Directories are processed recursively, collecting files with
50: * <code>suffix</code>. If <code>name</code> refers to a file, it is simply
51: * added to the list.
52: */
53: public static String[] getFileList(String[] names, String suffix) {
54: List fileList = new ArrayList();
55: for (int i = 0; i < names.length; ++i) {
56: getFileList(fileList, names[i], suffix);
57: }
58: String[] fileNames = (String[]) fileList.toArray(new String[0]);
59: return fileNames;
60: }
61:
62: }
|