用于删除java注释的小程序

只能删除java文件中每一行中第一次出现的/**/注释
对java反编译以后的代码注释删除工作有帮助

删除java单行注释

01 import java.io.BufferedReader;
02 import java.io.BufferedWriter;
03 import java.io.File;
04 import java.io.FileOutputStream;
05 import java.io.FileReader;
06 import java.io.FileWriter;
07 import java.io.OutputStreamWriter;
08  
09 class ListAllFirst {
10     public static void main(String[] args) throws Exception {
11         String fileName = "D:\\dto.syntheticalstatistics.src\\com\\sysway\\boss\\dto\\report\\statistics";
12         File f = new File(fileName);
13         print(f);
14  
15     }
16  
17     public static void print(File f) throws Exception {
18  
19         if (f != null) {
20             if (f.isDirectory()) {
21                 String st2 = f.toString().replace("D:\\", "E:\\");
22                 File fc = new File(st2);
23                 fc.mkdirs();
24                 File[] fileArray = f.listFiles();
25                 if (fileArray != null) {
26                     for (int i = 0; i < fileArray.length; i++) {
27                         // 递归调用
28                         print(fileArray[i]);
29                     }
30                 }
31             } else {
32                 // System.out.println(f);
33                 if (f.getName().endsWith(".java")) {
34                     String stfile = f.toString().replace("D:\\", "E:\\");
35  
36                     FileOutputStream fileOutputStream = new FileOutputStream(
37                             stfile);
38                     OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
39                             fileOutputStream, "UTF-8");// 或者"GBK"
40                     BufferedWriter bw = new BufferedWriter(outputStreamWriter);
41  
42                     // BufferedWriter bw=new BufferedWriter(new
43                     // FileWriter(stfile));
44                     String str = DelCommentsInJava.fun1(f);
45                     bw.write(str);
46                     bw.flush();
47                     bw.close();
48  
49                 } else {
50                     String out = f.toString().replace("D:\\", "E:\\");
51                     BufferedReader reader = new BufferedReader(
52                             new FileReader(f));
53                     StringBuilder content = new StringBuilder();
54                     String tmp = null;
55                     while ((tmp = reader.readLine()) != null) {
56                         content.append(tmp);
57                         content.append("\n");
58                     }
59                     String target = content.toString();
60                     BufferedWriter bw = new BufferedWriter(new FileWriter(out));
61                     bw.write(target);
62                     bw.flush();
63                     bw.close();
64                     reader.close();
65                 }
66             }
67         }
68     }
69  
70 }

你可能感兴趣的:(java)