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

1    package futils; 
2     
3    import javax.swing.JFileChooser; 
4    import javax.swing.JFrame; 
5    import java.awt.FileDialog; 
6    import java.awt.Frame; 
7    import java.io.BufferedWriter; 
8    import java.io.File; 
9    import java.io.FileOutputStream; 
10   import java.io.FileReader; 
11   import java.io.FileWriter; 
12   import java.io.IOException; 
13   import java.io.PrintWriter; 
14   import java.io.StreamTokenizer; 
15    
16   public final class WriterUtil { 
17       /** 
18        * Don't let anyone instantiate this class. 
19        */ 
20       private WriterUtil() { 
21       } 
22    
23    
24       // Some versions of windows will 
25   // create a .* suffix on a file name 
26   // The following code will strip it: 
27       public static String FilterFileNameBug( 
28               String fname) { 
29           if (fname.endsWith(".*.*")) { 
30               fname = 
31               fname.substring(0, 
32                               fname.length() - 4); 
33           } 
34           return fname; 
35       } 
36    
37       public static String getWriteDirectoryName( 
38               String prompt) { 
39           FileDialog dialog = new FileDialog( 
40                   new Frame(), 
41                   prompt, 
42                   FileDialog.SAVE); 
43           dialog.show(); 
44           String fs = dialog.getDirectory(); 
45           dialog.dispose(); 
46           return FilterFileNameBug(fs); 
47       } 
48    
49    
50       public static File getDirFileAWT( 
51               String prompt) { 
52           FileDialog fd = 
53                   new FileDialog(new Frame(), 
54                                  prompt); 
55           fd.show(); 
56           String dirName = fd.getDirectory(); 
57           fd.dispose(); 
58           return new File(dirName); 
59       } 
60    
61       public static File JGetDirFile(String prompt) { 
62           JFileChooser jfc = new JFileChooser(); 
63           jfc.showOpenDialog(new JFrame(prompt)); 
64           File f = jfc.getSelectedFile(); 
65           return f.getParentFile(); 
66       } 
67    
68       public static File getDirFile(String prompt) { 
69           if (Futil.isSwing()) 
70               return JGetDirFile(prompt); 
71           return getDirFileAWT(prompt); 
72       } 
73    
74    
75       public static void writeFilteredHrefFile( 
76               String inputFileName, 
77               String outputFileName) { 
78           System.out.println("Filtering:\t" + 
79                              inputFileName + 
80                              "\t>\t" + 
81                              outputFileName); 
82           try { 
83               FileReader fr = new FileReader( 
84                       inputFileName); 
85               StreamTokenizer st = 
86                       new StreamTokenizer(fr); 
87    
88    
89               FileOutputStream fos = new FileOutputStream( 
90                       outputFileName); 
91               PrintWriter output = new PrintWriter( 
92                       fos); 
93               int i; 
94               int next = 0; 
95               st.resetSyntax(); 
96               st.wordChars(0, 255); 
97               st.quoteChar('"'); 
98               while ((next = st.nextToken()) != 
99                      StreamTokenizer.TT_EOF) { 
100                  switch (next) { 
101                      case '"': 
102                          output.print('"'); 
103                          for (i = 0; i < 
104                                      st.sval.length(); i++) 
105                              if (st.sval.charAt(i) == 
106                                  ' ') 
107                                  output.print( 
108                                          "%20"); 
109                              else 
110                                  output.print( 
111                                          st.sval.charAt( 
112                                                  i)); 
113                          output.print('"'); 
114                          break; 
115                      case StreamTokenizer.TT_WORD: 
116                          output.print( 
117                                  st.sval + " "); 
118                          break; 
119                      case StreamTokenizer.TT_NUMBER: 
120                          output.print( 
121                                  st.nval + " "); 
122                          break; 
123                      case StreamTokenizer.TT_EOL: 
124                          output.println(); 
125                          break; 
126                  } // end switch 
127              } // end while 
128              fr.close(); 
129              fos.close(); 
130          } // end try 
131          catch (Exception exe) { 
132              System.out.println( 
133                      "writeFilteredHrefFile:er!"); 
134          } 
135      } 
136   
137      public static final char CARRAGE_RETURN = 13; 
138      public static final char LINE_FEED = 10; 
139      public static final String NEW_LINE = "" 
140                                            + 
141                                            CARRAGE_RETURN + 
142                                            LINE_FEED; 
143   
144      public static void main(String[] args) { 
145          testWriteString(); 
146   
147      } 
148   
149   
150      public void lowerFileNames(File thePath) { 
151          String[] fileNames = thePath.list(); 
152          String pathstr = thePath.getPath(); 
153          for (int i = 0; 
154               fileNames != null && 
155               i < fileNames.length; i++) { 
156              String aFileName = fileNames[i]; 
157              String newFileName = aFileName.toLowerCase(); 
158              File theFile = new File(pathstr, 
159                                      aFileName); 
160              if (theFile.isFile()) { 
161                  //rename theFile to lower case 
162                  System.out.print( 
163                          i + ":" + aFileName); 
164                  theFile.renameTo(new File( 
165                          pathstr, 
166                          newFileName)); 
167                  System.out.println( 
168                          "\t==>\t" + newFileName); 
169              } else { 
170                  //case theFile is Dir, in the Dir, repeat same procedure 
171                  System.out.println( 
172                          "Dir:" + aFileName); 
173                  lowerFileNames( 
174                          new File( 
175                                  pathstr + 
176                                  aFileName)); 
177              } 
178          } 
179          return; 
180      }//lowerFileNames 
181   
182      /** 
183       * Convert a file to a FileWriter 
184       * 
185       * @param f The input File 
186       * @return A FileWriter obtained from the 
187       *         file 
188       */ 
189      public static FileWriter getFileWriter( 
190              File f) { 
191          try { 
192              return new FileWriter(f); 
193          } catch (IOException e) { 
194              e.printStackTrace(); 
195              return null; 
196          } 
197      } 
198   
199      public static void testGetCSVString() { 
200          int ia[] = { 
201              1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 11, 12, 13, 14 
202          }; 
203          System.out.println(getCSVString(ia)); 
204      } 
205   
206      public static String getCSVString(int ia[]) { 
207          StringBuffer sb = new StringBuffer(); 
208          for (int i = 0; i < ia.length; i++) { 
209              sb.append("" + ia[i] + NEW_LINE); 
210          } 
211          return sb.toString(); 
212      } 
213   
214      public static void testWriteString() { 
215          writeString( 
216                  "This is a test of write string" + 
217                  NEW_LINE 
218                  + " this is more stuff" + 
219                  NEW_LINE 
220                  + 
221                  "I hope these newlines come out ok"); 
222          System.out.println( 
223                  "testWriteString ends"); 
224      } 
225   
226      /** 
227       * Write a string out to a file. Prompts the 
228       * user for a file name ouputs the string to 
229       * the file, then closes the file. 
230       * 
231       * @param s The string to write out. 
232       */ 
233      public static void writeString(String s) { 
234          FileWriter fw = getFileWriter( 
235                  "enter output file"); 
236          try { 
237              fw.write(s); 
238              fw.close(); 
239          } catch (IOException e) { 
240              e.printStackTrace(); 
241          } 
242      } 
243   
244      /** 
245       * Write a string out to a file. Prompts the 
246       * user for a file name ouputs the string to 
247       * the file, then closes the file. 
248       * 
249       * @param s The string to write out. 
250       */ 
251      public static void writeString(String s[]) { 
252          FileWriter fw = getFileWriter( 
253                  "enter output file"); 
254          try { 
255              for (int i = 0; i < s.length; i++) 
256                  fw.write(s[i]); 
257   
258              fw.close(); 
259          } catch (IOException e) { 
260              e.printStackTrace(); 
261          } 
262      } 
263   
264      public static void writeString2(File f, 
265                                      String s) { 
266          try { 
267              FileOutputStream fos = new FileOutputStream( 
268                      f); 
269              FileWriter fw = new FileWriter(f); 
270              fw.write(s); 
271              fw.close(); 
272              fos.close(); 
273          } catch (IOException e) { 
274              e.printStackTrace(); 
275          } 
276      } 
277   
278      /** 
279       * prompt the user for a file and return a 
280       * writer. 
281       * 
282       * @param prompt the string prompt displayed 
283       *               to the user. 
284       * @return A FileWriter obtained from a use 
285       *         selection 
286       */ 
287      public static FileWriter getFileWriter( 
288              String prompt) { 
289          return getFileWriter( 
290                  Futil.getWriteFile(prompt)); 
291      } 
292   
293      /** 
294       * "@param prompt 
295       */ 
296      public static BufferedWriter 
297              getBufferedWriter(String prompt) { 
298          File f = Futil.getWriteFileSwing(prompt); 
299          return getBufferedWriter(f); 
300      } 
301   
302      /** 
303       * "@param f 
304       */ 
305      public static BufferedWriter 
306              getBufferedWriter(File f) { 
307          BufferedWriter bw = null; 
308          try { 
309              bw = new BufferedWriter( 
310                      new FileWriter(f)); 
311          } catch (IOException e) { 
312          } 
313          return bw; 
314      } 
315   
316      /** 
317       * "@param bw 
318       */ 
319      public static void close(BufferedWriter bw) { 
320          try { 
321              bw.close(); 
322          } catch (IOException e) { 
323              e.printStackTrace(); 
324          } 
325      } 
326   
327      public static String getWriteDirectoryName() { 
328          FileDialog dialog = new FileDialog( 
329                  new Frame(), 
330                  "Enter file name", 
331                  FileDialog.SAVE); 
332          dialog.show(); 
333          String fs = dialog.getDirectory(); 
334          System.out.println("Opening file: " + fs); 
335          dialog.dispose(); 
336          return Futil.FilterFileNameBug(fs); 
337      } 
338   
339      public static void close(FileWriter fw) { 
340          try { 
341              fw.close(); 
342          } catch (IOException e) { 
343              e.printStackTrace(); 
344          } 
345      } 
346   
347      public static File[] getFiles(String prompt) { 
348          File readFile = Futil.getReadFile(prompt); 
349          System.out.println("readFile=" + 
350                             readFile); 
351          File parent = readFile.getParentFile(); 
352          return 
353                  parent.listFiles( 
354                          new FileFilter()); 
355      } 
356   
357      /** 
358       * "@param bw 
359       * 
360       * @param bw 
361       * @param o 
362       */ 
363      public static void println(BufferedWriter bw, 
364                                 Object o) { 
365          try { 
366              bw.write(o + "\n"); 
367          } catch (IOException e) { 
368              e.printStackTrace(); 
369          } 
370      } 
371   
372      public static String getSaveFileName( 
373              String prompt) { 
374          FileDialog fd = new 
375                  FileDialog(new Frame(), 
376                             prompt, 
377                             FileDialog.SAVE); 
378          fd.setVisible(true); 
379          fd.setVisible(false); 
380          String fn = fd.getDirectory() + 
381                      fd.getFile(); 
382          if (fd.getFile() == null) return null; //usr canceled 
383          return fn; 
384      } 
385   
386      public static String getSaveName( 
387              String prompt) { 
388          FileDialog fd = new 
389                  FileDialog(new Frame(), 
390                             prompt, 
391                             FileDialog.SAVE); 
392          fd.setVisible(true); 
393          fd.setVisible(false); 
394          String fn = fd.getFile(); 
395          if (fd.getFile() == null) return null; //usr canceled 
396          return fn; 
397      } 
398   
399      public static String getSaveDirectoryName( 
400              String prompt) { 
401          FileDialog fd = new 
402                  FileDialog(new Frame(), 
403                             prompt, 
404                             FileDialog.SAVE); 
405          fd.setVisible(true); 
406          fd.setVisible(false); 
407          return fd.getDirectory(); 
408      } 
409   
410   
411  } 
412