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

1    package futils; 
2     
3    import java.io.*; 
4     
5     
6    public final class Cat { 
7     
8        private Cat() { 
9        } 
10    
11       static public void toConsole2(String suffix) { 
12           File files[] = Ls.getWildFiles(suffix); 
13           for (int i = 0; i < files.length; i++) 
14               Cat.toConsole(ReaderUtil.getBufferedReader( 
15                       files[i])); 
16       } 
17    
18    
19       static public void toFile(String suffix) { 
20           File files[] = Ls.getWildFiles(suffix); 
21           BufferedWriter bw = 
22                   WriterUtil.getBufferedWriter("select an output for the *" + suffix + " files"); 
23           for (int i = 0; i < files.length; i++) 
24               fileToWriter(files[i], bw); 
25           WriterUtil.close(bw); 
26       } 
27    
28       /** 
29        *  use the suffix to select a bunch of files and 
30        * write these files to a single file. 
31        */ 
32       static public void wildToFile(String suffix, FileWriter fw) { 
33    
34           try { 
35               File files[] = Ls.getWildFiles(suffix); 
36               if (files == null) { 
37                   System.out.println("ER!:wildToFile, files==null"); 
38                   return; 
39               } 
40               System.out.println("will process " + files.length + " files"); 
41               PrintWriter pw = new PrintWriter(fw); 
42               for (int i = 0; i < files.length; i++) 
43                   fileToStream( 
44                           new BufferedReader( 
45                                   new FileReader(files[i])), pw); 
46               fw.close(); 
47           } catch (IOException e) { 
48               e.printStackTrace(); 
49           } 
50       } 
51    
52       public static void testWildToFile() { 
53           wildToFile(".java", 
54                   WriterUtil.getFileWriter("select output file")); 
55       } 
56    
57       static public void javasToConsole2() { 
58           File files[] = Ls.getWildFiles("java"); 
59           for (int i = 0; i < files.length; i++) 
60               fileToConsole( 
61                       files[i]); 
62       } 
63    
64    
65       static public void fileToConsole(File f) { 
66           try { 
67               System.out.println("cat: " + f); 
68               FileReader fr = new FileReader(f); 
69               String line; 
70               BufferedReader br = new BufferedReader(fr); 
71               while ((line = br.readLine()) != null) 
72                   System.out.println(line); 
73               br.close(); 
74           } // try 
75           catch (Exception exe) { 
76               System.out.println("cat:Error on input file"); 
77           } 
78       } 
79    
80       static public void fileToWriter(File f, BufferedWriter bw) { 
81           System.out.println("cat: " + f); 
82           BufferedReader br = 
83                   ReaderUtil.getBufferedReader(f); 
84           for (String line = ReaderUtil.readLine(br); 
85                line != null; line = ReaderUtil.readLine(br)) { 
86               WriterUtil.println(bw, line); 
87           } 
88           ReaderUtil.close(br); 
89       } 
90    
91       public static void toConsole() { 
92           toConsole(ReaderUtil.getBufferedReader( 
93                   Futil.getReadFile( 
94                           "select a file to Cat.listFilesToConsole()"))); 
95       } 
96    
97       static public void toConsole(BufferedReader br) { 
98           String line; 
99           while ((line = ReaderUtil.readLine(br)) != null) 
100              System.out.println(line); 
101          ReaderUtil.close(br); 
102      } 
103   
104      static public void fileToStream( 
105              BufferedReader br, PrintWriter pw) { 
106          String line; 
107          try { 
108   
109              while ((line = br.readLine()) != null) 
110                  pw.println(line); 
111              br.close(); 
112          } // try 
113          catch (Exception exe) { 
114              System.out.println("cat:Error on input file"); 
115          } 
116      } 
117   
118      public static void main(String args[]) { 
119          testWildToFile(); 
120          System.out.println("done!"); 
121      } 
122   
123  }