Futil.java
1. package futils;
2. import java.util.*;
3. import java.awt.*;
4. import java.io.*;
5.
6. public final class Futil {
7. /**
8. * Don't let anyone instantiate this class.
9. */
10. private Futil() {}
11.
12.
13.
14. public static String getReadFileName(String prompt) {
15. Frame f = new Frame();
16. f.setSize(100,100);
17. FileDialog fd = new FileDialog(f, prompt);
18. fd.show();
19. String file_name = fd.getFile();
20. String path_name = fd.getDirectory();
21. String file_string = path_name + file_name;
22. fd.dispose();
23. return file_string;
24. }
25. public static String getReadFileName() {
26. return getReadFileName("Select File to read");
27. }
28.
29. public static File getReadFile() {
30. return new File(getReadFileName());
31. }
32.
33.
34. public static FileOutputStream getWriteFileOutputStream() {
35. try {
36. return
37. new FileOutputStream(getWriteFileName());
38. }
39. catch (Exception e) {
40. System.out.println("Er: FileOutputStream in futils.java");}
41. return null;
42. }
43.
44. public static String getWriteFileName(String prompt) {
45. FileDialog fd = new FileDialog(new Frame(),
46. "Enter file name", FileDialog.SAVE);
47. fd.show();
48. String fs = fd.getDirectory() + fd.getFile();
49. fd.dispose();
50. return FilterFileNameBug(fs);
51. }
52. public static String getWriteFileName() {
53. return getWriteFileName("select file for output");
54. }
55. public static File getWriteFile() {
56. return new File(getWriteFileName());
57. }
58.
59. public static String getWriteDirectoryName() {
60. FileDialog dialog = new FileDialog(new Frame(), "Enter file name",FileDialog.SAVE);
61. dialog.show();
62. String fs = dialog.getDirectory();
63. System.out.println("Opening file: "+fs);
64. dialog.dispose();
65. return FilterFileNameBug(fs);
66. }
67.
68. // Some versions of windows will
69. // create a .* suffix on a file name
70. // The following code will strip it:
71. public static String FilterFileNameBug(String fname) {
72. if (fname.endsWith(".*.*")) {
73. fname=fname.substring(0, fname.length() - 4);
74. }
75. return fname;
76. }
77.
78.
79. public static File getDirFile() {
80. return new File(Ls.getDirName());
81. }
82.
83.
84. public static FileInputStream getFileInputStream() {
85. return getFileInputStream(getReadFileName());
86. }
87.
88. public static FileInputStream getFileInputStream(String name) {
89. FileInputStream fis = null;
90. try
91. {fis = new FileInputStream(name);}
92. catch (IOException e)
93. {System.out.println("futil:Could not open file");}
94. return fis;
95. }
96. public static FileInputStream getFileInputStream(File file) {
97. FileInputStream fis = null;
98. try
99. {fis = new FileInputStream(file);}
100. catch (IOException e)
101. {System.out.println("futil:Could not open file");}
102. return fis;
103. }
104.
105. public static FileOutputStream getFileOutputStream() {
106. FileOutputStream fos = null;
107. try {fos =
108. new FileOutputStream(getWriteFileName());
109. }
110. catch (IOException e) {
111. System.out.println("futil:Could not create file");
112. }
113. return fos;
114. }
115.
116. // Open the file, return -1 if file cannot be opened
117. // otherwise return the size in bytes
118. public static int available(File file) {
119. FileInputStream fis = null;
120. int sizeInBytes = -1;
121. try {
122. fis = new FileInputStream(file);
123. sizeInBytes = fis.available();
124. fis.close();
125. }
126. catch (IOException e)
127. {System.out.println("Futil:Could not open file");}
128. return sizeInBytes;
129. }
130.
131. public static void closeOutputStream(OutputStream os) {
132. try {os.close();} // end try
133. catch (IOException exe)
134. {System.out.println("futil: could not close output stream");}
135. }
136.
137. public static void closeInputStream(InputStream is) {
138. try {is.close();} // end try
139. catch (IOException exe)
140. {System.out.println("futil: could not close input stream");}
141. }
142.
143.
144.
145. public static void lsWordPrintMerge() {
146. String wild = "pict";
147.
148. String[] files = Ls.getWildNames(wild);
149.
150. System.out.println(files.length + " file(s):");
151.
152. for (int i=0; i < files.length; i++)
153. System.out.println("\t" + files[i]);
154.
155. }
156.
157.
158.
159.
160. static public void filterHtmls() {
161. String[] files = Ls.getWildNames(".html");
162. File input_dir = getDirFile();
163. System.out.println(files.length + " file(s) to process");
164.
165. File output_dir = new File(input_dir.getParent(),"out");
166. if (output_dir.exists())
167. {System.out.println("Output dir exists:"+ output_dir);}
168. else {output_dir.mkdir();}
169.
170. for (int i=0; i < files.length; i++) {
171. writeFilteredHrefFile(input_dir+files[i],output_dir+"/"+files[i]);
172. }
173. }
174.
175. public static void writeFilteredHrefFile(String inputName, String outputName) {
176. System.out.println("Filtering:\t" + inputName +"\t>\t"+outputName);
177. try {
178. FileInputStream is = new FileInputStream(inputName);
179. StreamTokenizer tokens = new StreamTokenizer(is);
180. FileOutputStream os = new FileOutputStream(outputName);
181. PrintStream output = new PrintStream(os);
182. int i;
183. int next = 0;
184. tokens.resetSyntax();
185. tokens.wordChars(0,255);
186. tokens.quoteChar('"');
187. while ((next = tokens.nextToken()) != tokens.TT_EOF) {
188. switch (next) {
189. case '"':
190. output.print('"');
191. for (i=0;i<tokens.sval.length();i++)
192. if (tokens.sval.charAt(i) == ' ')
193. output.print("%20");
194. else
195. output.print(tokens.sval.charAt(i));
196. output.print('"');
197. break;
198. case tokens.TT_WORD:
199. output.print(tokens.sval+" ");
200. break;
201. case tokens.TT_NUMBER:
202. output.print(tokens.nval+" ");
203. break;
204. case tokens.TT_EOL:
205. output.println();
206. break;
207. } // end switch
208. } // end while
209. is.close();
210. os.close();
211. } // end try
212. catch (Exception exe)
213. {System.out.println("writeFilteredHrefFile:er!");}
214. }
215.
216. public static void main(String[] args) {
217. String name = getReadFileName( ) ;
218. System.out.println("HEllo world, I got:"+name);
219. try {
220. System.in.read(); // prevent console window from going away
221. } catch (java.io.IOException e) {}
222.
223. }
224. public static void writeFilteredJavaFile( ) {
225. try {
226. FileInputStream is = getFileInputStream();
227. StreamTokenizer tokens = new StreamTokenizer(is);
228. PrintStream output = System.out;
229. int i;
230. int next = 0;
231. tokens.resetSyntax();
232. tokens.wordChars(0,255);
233. tokens.quoteChar(';');
234. while ((next = tokens.nextToken()) != tokens.TT_EOF) {
235. switch (next) {
236. case ';':
237.
238. output.print("got a line:"+tokens.lineno());
239. break;
240. case tokens.TT_WORD:
241. output.print(tokens.sval+" ");
242. break;
243. case tokens.TT_NUMBER:
244. output.print(tokens.nval+" ");
245. break;
246. case tokens.TT_EOL:
247. output.println();
248. break;
249. } // end switch
250. } // end while
251. is.close();
252. } // end try
253. catch (Exception exe)
254. {System.out.println("writeFilteredHrefFile:er!");}
255. }
256.
257. public static void list_filtered_href_file(String file) {
258. System.out.println("processing:\t" + file);
259. try {
260. FileInputStream input_file = new
261. FileInputStream(file);
262. StreamTokenizer tokens = new StreamTokenizer(input_file);
263. int next = 0;
264. tokens.resetSyntax();
265. tokens.wordChars(0,255);
266. tokens.quoteChar('"');
267. while ((next = tokens.nextToken()) != tokens.TT_EOF)
268. {
269. switch (next) {
270. case '"':
271. System.out.print('"');
272. StringTokenizer st =
273. new StringTokenizer(tokens.sval," ");
274. while (st.hasMoreTokens()) {
275. System.out.print(st.nextToken());
276. if (st.countTokens() > 1)
277. {System.out.print("%20");}
278. }
279. System.out.print('"');
280. break;
281. case tokens.TT_WORD:
282. System.out.print(tokens.sval+" ");
283. break;
284. case tokens.TT_NUMBER:
285. System.out.print(tokens.nval+" ");
286. break;
287. case tokens.TT_EOL:
288. System.out.println();
289. break;
290. }
291. }
292. System.out.println();
293. input_file.close();
294. }
295. catch (Exception exe)
296. {System.out.println("list_filtered_href_file:er!");}
297. }
298.
299. public static void filterFileHrefs() {
300. FileInputStream fis = getFileInputStream();
301. StreamTokenizer tokens = new StreamTokenizer(fis);
302. int next = 0;
303. tokens.resetSyntax();
304. tokens.wordChars(0,255);
305. tokens.quoteChar('"');
306.
307. try { while ((next = tokens.nextToken())
308. != tokens.TT_EOF) {
309. switch (next) {
310. case '"':
311. System.out.print('"');
312. StringTokenizer st =
313. new StringTokenizer(tokens.sval," ");
314. while (st.hasMoreTokens()) {
315. System.out.print(st.nextToken());
316. if (st.countTokens() > 1)
317. {System.out.print("%20");}
318. }
319. System.out.print('"');
320. break;
321. case tokens.TT_WORD:
322. System.out.print(tokens.sval+" ");
323. break;
324. case tokens.TT_NUMBER:
325. System.out.print(tokens.nval+" ");
326. break;
327. case tokens.TT_EOL:
328. System.out.println();
329. break;
330. } // switch
331. }} // try while
332. catch (IOException e) {
333. System.out.println("Futil:ER! filterFileHrefs");
334. }
335. System.out.println();
336. closeInputStream(fis);
337. }
338.
339.
340. public static void processJava() {
341. FileInputStream fis = getFileInputStream();
342. String line;
343. DataInputStream dis = new DataInputStream(fis);
344. System.out.println("<HTML><BODY><PRE>");
345. try { while ((line = dis.readLine()) != null)
346. System.out.println(line);
347. }
348. catch (IOException e) {
349. System.out.println("Futil: ER! in processJava");
350. }
351. System.out.println("</PRE></BODY></HTML>");
352. closeInputStream(fis);
353. }
354.
355. public void lowerFileNames( File thePath ){
356. String[] fileNames = thePath.list();
357. String pathstr = thePath.getPath();
358. for( int i=0;
359. fileNames != null &&
360. i< fileNames.length; i++ ) {
361. String aFileName = fileNames[ i ];
362. String newFileName = aFileName.toLowerCase();
363. File theFile = new File( pathstr, aFileName );
364. if( theFile.isFile() ){
365. //rename theFile to lower case
366. System.out.print( i+":" + aFileName );
367. theFile.renameTo( new File( pathstr, newFileName ) );
368. System.out.println( "\t==>\t"+newFileName );
369. }else{
370. //case theFile is Dir, in the Dir, repeat same procedure
371. System.out.println( "Dir:"+aFileName );
372. lowerFileNames( new File( pathstr+aFileName ));
373. }
374. }
375. return;
376. }//lowerFileNames
377.
378.
379. public static void makeTocHtml() {
380. File dir = getDirFile();
381. String[] files = dir.list(new FileFilter());
382. System.out.println(files.length + " file(s):");
383. FileOutputStream fos = getFileOutputStream();
384. PrintStream ps = new PrintStream(fos);
385. ps.println("<HTML>");
386. ps.println("<BODY>");
387. ps.println("<ul>");
388. for (int i=0; i < files.length; i++)
389. ps.println("<LI><a href = \"" + files[i]+"\">"+
390. files[i]+"</a><P>");
391. ps.println("</ul>");
392. ps.println("</BODY>");
393. ps.println("</HTML>");
394. closeOutputStream(fos);
395. }
396.
397.
398. public static void readDataFile(String file,double data[]) {
399. System.out.println("processing:\t" + file);
400. FileInputStream inputFile =
401. getFileInputStream(file);
402. StreamTokenizer tokens = new StreamTokenizer(inputFile);
403. int next = 0;
404. int num = 0;
405. try {
406. while ((next = tokens.nextToken()) != tokens.TT_EOF) {
407. switch (next) {
408. case tokens.TT_WORD:
409. break;
410. case tokens.TT_NUMBER:
411. data[num] = (double) tokens.nval;
412. System.out.println(num+": "+ data[num]);
413. num = num + 1;
414. break;
415. case tokens.TT_EOL:
416. break;
417. }
418. }
419.
420. }
421. catch (Exception exe)
422. {System.out.println("listFilteredHrefFile:er!");}
423. closeInputStream(inputFile);
424. }
425.
426. }