/Users/lyon/j4p/src/futils/Ls.java
|
1 package futils;
2
3 import java.io.File;
4
5
6 /** A class that contains methods for doing batch file processing
7 *
8 * @version 1.0
9 */
10 public final class Ls {
11
12 /** prevent instantiation of this class.
13 */
14 private Ls() {
15 }
16
17
18 /** prompt the user for a file and return all the directories
19 * in that location
20 *
21 * @return An array of all the directory names
22 */
23 public static File[] getDirFiles() {
24 return getDirFiles(
25 Futil.getReadFile("select a file"));
26 }
27
28 /** given a file name, get all the directories in that location.
29 *
30 * @param startFile start point of a traversal of the file system
31 * @return a list of all the directory names
32 */
33 static public File[] getDirFiles(File startFile) {
34 return startFile.listFiles(new DirFilter());
35 }
36
37 /** Print the directories in a uses specified location
38 */
39 static public void DirsToConsole() {
40 System.out.println("Ls.DirsToConsole");
41 File[] dirs = getDirFiles();
42 System.out.println(dirs.length + " dir(s):");
43 for (int i = 0; i < dirs.length; i++)
44 System.out.println("\t" + dirs[i]);
45 }
46
47
48 static public String[] getWildNames(File dir, String prefix, String suffix) {
49 String absPath = dir.getAbsolutePath();
50 String[] fileNames = dir.list(
51 new WildFilter(prefix, suffix));
52 String sep = System.getProperty("file.separator");
53 System.out.println("getWildNames:" + absPath);
54 for (int i = 0; i < fileNames.length; i++) {
55 fileNames[i] = absPath + sep + fileNames[i];
56 }
57 return fileNames;
58 }
59
60
61 public static void testGetWildFiles() {
62 File f[] = getWildFiles(
63 Futil.getReadDirFile(
64 "select a class directory"), "class");
65 System.out.println("f.length=" + f.length);
66 print(f);
67 }
68
69 static public String[] getWildNames(String prefix,
70 String suffix) {
71 File dir = Futil.getReadFile("select a file");
72 return getWildNames(dir, prefix, suffix);
73 }
74
75 static public void wildToConsole(String wild) {
76 File files[] = getWildFiles(wild);
77 System.out.println(files.length + " file(s):");
78 for (int i = 0; i < files.length; i++)
79 System.out.println("\t" + files[i]);
80
81 }
82
83
84 /** print an array of objects to the console
85 *
86 * @param o an array of objects
87 */
88 public static void print(Object o[]) {
89 for (int i = 0; i < o.length; i++)
90 System.out.println(o[i]);
91 }
92
93 static public void listFilesToConsole(String suffix) {
94
95 File dirFile = Futil.getReadFile("select a file");
96 String[] files = dirFile.list(new WildFilter(suffix));
97 System.out.println(files.length + " file(s):");
98 for (int i = 0; i < files.length; i++)
99 System.out.println("\t" + files[i]);
100 }
101
102 static public void deleteWildFiles(String wild) {
103 File[] files = getWildFiles(wild);
104 System.out.println(files.length + " file(s):");
105 for (int i = 0; i < files.length; i++)
106 deleteFile(files[i]);
107 }
108
109 static public void deleteFile(File f) {
110
111 System.out.print("deleting file " + f);
112 if (f.exists()) {
113 System.out.println(" deleted!");
114 f.delete();
115 } else
116 System.out.println(" does not exist");
117 }
118
119 /**
120 * gets all the files that end with a PICT suffix
121 * and creates a WORD print merge command that will include the pict files.
122 */
123
124 public static void wordPrintMerge() {
125 String wild = "";
126 String wordString =
127 ". ÇINCLUDE hd:current:Java book:chapter I:batch 1 rev1:picts:";
128 File files[] = getWildFiles(wild);
129 System.out.println(files.length + " file(s):");
130 int fileNumber;
131 for (int i = 0; i < files.length; i++) {
132 fileNumber = i + 1;
133 System.out.print("Figure *." +
134 fileNumber +
135 wordString);
136 System.out.println(files[i] + "È");
137 }
138 }
139
140
141
142
143 static public File[] getDirs(File dir) {
144 return dir.listFiles(new DirFilter());
145 }
146
147
148 public static File[] getWildFiles(String suffix) {
149 return getWildFiles(Futil.getReadFile("select a file"),
150 suffix);
151 }
152
153 static public File[] getWildFiles(File directory, String suffix) {
154 String absPath = directory.getAbsolutePath();
155 if (directory.isFile()) {
156 directory = directory.getParentFile();
157 }
158 return directory.listFiles(new WildFilter(suffix));
159 }
160
161
162 static public void toConsole() {
163
164 File dirFile = Futil.getReadDirFile("select a file");
165 String[] files = dirFile.list(new FileFilter());
166 System.out.println(files.length + " file(s):");
167 for (int i = 0; i < files.length; i++)
168 System.out.println("\t" + files[i]);
169 }
170
171
172 /** "@param args
173 */
174 public static void main(String args[]) {
175 testGetWildFiles();
176 }
177 }
178