/Users/lyon/j4p/src/ip/hak/CopyDir.java

1    package ip.hak; 
2     
3    import java.awt.*; 
4    import java.io.*; 
5    import java.util.Vector; 
6     
7    public class CopyDir extends ip.gui.frames.ClosableFrame { 
8        String sd = null;// Source Directory 
9        String dd = null;// Destination Directory 
10       String ifiles[]; 
11    
12       public CopyDir(String title) { 
13           super(title); 
14           init(); 
15       } 
16    
17       public static void main(String args[]) { 
18           CopyDir cd = new CopyDir("Copy Directory Program"); 
19       } 
20    
21       public void init() { 
22           // get source & destination directory 
23           // to select the directory you have to select any file 
24           // directory 
25           sd = getDir("Select Source Directory"); 
26           dd = getDir("Select Destination Directory"); 
27    
28           /* 
29           // test only 
30           sd="D:\\TEST\\TEMP1"; 
31           dd="D:\\TEST\\TEMP2"; 
32           */ 
33    
34           if (sd == null || dd == null) 
35               return; 
36    
37           ifiles = getInputFiles(sd); 
38           if (ifiles == null) 
39               return; 
40           InputDialog id = new InputDialog(this, "", true, ifiles, sd, dd); 
41           String iv[] = id.getInput(); 
42           if (iv == null) 
43               changeString(null, null, null, null); 
44           else 
45               changeString(iv[0], iv[1], iv[2], iv[3]); 
46    
47       } 
48    
49       int counter = 0; 
50    
51       public void changeString(String inputName, String outputName, String searchString, String replaceString) { 
52           for (int i = 0; i < ifiles.length; i++) { 
53               if (inputName == null || !inputName.equals(ifiles[i])) 
54                   copyFile(sd + ifiles[i], dd + ifiles[i]); 
55               else 
56                   copyFile(sd + inputName, dd + outputName, searchString, replaceString); 
57           } 
58    
59           ErrorDialog ed = new ErrorDialog(this, "Result summery", 
60                   "Total " + counter + " string(s) replaced"); 
61       } 
62    
63       public void copyFile(String s, String d) { 
64           String line = null; 
65           try { 
66               FileReader fr = new FileReader(s); 
67               FileWriter fw = new FileWriter(d); 
68               BufferedReader br = new BufferedReader(fr); 
69    
70               while ((line = br.readLine()) != null) 
71                   fw.write(line + "\n"); 
72               br.close(); 
73               fw.close(); 
74               fr.close(); 
75    
76           } catch (IOException e) { 
77               System.out.println(e); 
78           } 
79       } 
80    
81    
82       public void copyFile(String s, String d, String ss, String rs) { 
83           String line = null; 
84           try { 
85               FileReader fr = new FileReader(s); 
86               FileWriter fw = new FileWriter(d); 
87               BufferedReader br = new BufferedReader(fr); 
88    
89               while ((line = br.readLine()) != null) { 
90                   line = replaceString(ss, rs, line); 
91                   fw.write(line + "\n"); 
92               } 
93               br.close(); 
94               fw.close(); 
95               fr.close(); 
96    
97           } catch (IOException e) { 
98               System.out.println(e); 
99           } 
100      } 
101   
102      public String replaceString(String ss, String rs, String line) { 
103          int lineSize = line.length(); 
104          int ssSize = ss.length(); 
105   
106          for (int i = 0; i < (lineSize - ssSize); i++) { 
107              if (line.startsWith(ss, i)) { 
108                  String part1 = line.substring(0, i); 
109                  String part2 = line.substring(i + ssSize); 
110                  counter++; 
111                  return part1 + rs + part2; 
112              } 
113          } 
114   
115          return line; 
116      } 
117   
118   
119      public String[] getInputFiles(String st) { 
120          File f = new File(st); 
121          String s1[] = f.list(); 
122   
123          if (s1 == null) 
124              return null; 
125          Vector v = new Vector(); 
126          for (int i = 0; i < s1.length; i++) { 
127              f = new File(st, s1[i]); 
128              if (f.isFile()) 
129                  v.addElement(s1[i]); 
130          } 
131   
132          String temp[] = new String[v.size()]; 
133          for (int i = 0; i < temp.length; i++) 
134              temp[i] = (String) v.elementAt(i); 
135   
136          return temp; 
137      } 
138   
139      public String getDir(String title) { 
140          FileDialog fd = new FileDialog(this, title); 
141          fd.setVisible(true); 
142          String fn = fd.getFile(); 
143   
144          if (fn == null) 
145              return null; 
146   
147          return fd.getDirectory(); 
148      } 
149  } 
150