/Users/lyon/j4p/src/futils/FileList.java

1    package futils; 
2     
3    import gui.dialogs.SelectorDialog; 
4    import gui.In; 
5     
6    import java.io.File; 
7    import java.util.Vector; 
8     
9    // futils.FileList 
10    
11   public class FileList { 
12    
13       // This vector will hold all the file names. 
14       private  Vector v = new Vector(); 
15    
16       public  File[] getFiles() { 
17           File f[] = new File[v.size()]; 
18           v.copyInto(f); 
19           return f; 
20       } 
21    
22       private static FileFilter ff = new FileFilter(); 
23       private static final DirFilter df = new DirFilter(); 
24    
25       // Recurse through all subdirectories and store 
26       // all the file names in a vector. 
27       public  void list(File directory) { 
28    
29           add(directory.listFiles(ff)); 
30           String dirs[] = directory.list(df); 
31    
32           if (dirs == null) return; 
33    
34           for (int i = 0; i < dirs.length; i++) { 
35               list(new File(directory, dirs[i])); 
36           } 
37       } 
38    
39       public  void list(File directory, WildFilter wf) { 
40    
41           add(directory.listFiles(wf)); 
42           String dirs[] = directory.list(df); 
43    
44           if (dirs == null) return; 
45    
46           for (int i = 0; i < dirs.length; i++) { 
47               list(new File(directory, dirs[i]), wf); 
48           } 
49       } 
50    
51       public  void delete() { 
52           File f[] = getFiles(); 
53           for (int i = 0; i < f.length; i++) { 
54               f[i].delete(); 
55           } 
56       } 
57    
58       private  void add(File f[]) { 
59           if (f != null) { 
60               for (int i = 0; i < f.length; i++) 
61                   v.addElement(f[i]); 
62           } 
63       } 
64    
65       public  void print() { 
66           int VectorSize = v.size(); 
67           for (int i = 0; i < VectorSize; i++) 
68               System.out.println(v.elementAt(i)); 
69       } 
70    
71       public  void deleteAllClassFiles() { 
72           File f = 
73                   Futil.getReadDirFile("select a directory for deletion (DANGER!!)"); 
74           WildFilter wf = new WildFilter(".class"); 
75           list(f, wf); 
76           File walkingDeadFiles [] = getFiles(); 
77           boolean b = In.getBoolean("do you really want to delete:"+walkingDeadFiles.length+"files?"); 
78           if (b) delete(walkingDeadFiles); 
79           else In.message("operation aborted"); 
80       } 
81       public static void delete(File f[]){ 
82           for (int i=0; i < f.length; i++) 
83               f[i].delete(); 
84           In.message("files deleted!"); 
85       } 
86    
87       public static void main(String args[]) { 
88           testFileLister(); 
89    
90       } 
91       public int getNumberOfFiles() { 
92           return v.size(); 
93       } 
94       private static void testFileLister() { 
95           FileList fl = new FileList(); 
96           File f = 
97                   Futil.getReadDirFile("select a start file"); 
98           WildFilter wf = new WildFilter(".jar"); 
99           fl.list(f, wf); 
100          fl.print(); 
101   
102          System.out.println("-----------------------"); 
103          System.out.println("Total number of files = " 
104                  + fl.getNumberOfFiles()); 
105          String s[] = SelectorDialog.getStringSelectorDialog(fl.getFiles()); 
106          for (int i=0; i < s.length; i++) 
107              System.out.println(s[i]); 
108   
109      } 
110  }