去掉注释

代码注释很重要,没有注释的代码简直就是噩梦。可就是有人想要没注释的源码,真无奈。。。

写了个小工具,去掉java中的代码注释,奉上。其实很简单,就是一些递归,遍历,IO

 

View Code
 1 package mxf.study.java;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.File;
6 import java.io.FileNotFoundException;
7 import java.io.FileReader;
8 import java.io.FileWriter;
9 import java.io.IOException;
10
11 public class RemoveJavaDoc {
12
13 private int count;
14 static String destPath = "C:/Java/src";
15 static String srcPath = "C:/Java/jdk1.6.0_22/src";
16
17 static {
18 File destPathFile = new File(destPath);
19 if(!destPathFile.exists()){
20 destPathFile.mkdir();
21 }
22 }
23 public static void main(String[] args) throws IOException {
24
25 RemoveJavaDoc rjd = new RemoveJavaDoc();
26 rjd.findFile(new File(srcPath));
27 System.out.println(rjd.count);
28 }
29
30 private void findFile(File file) throws IOException {
31 if (file.isDirectory()) {
32 String destFileName = destPath
33 + file.getAbsolutePath().substring(23);
34 new File(destFileName).mkdir();
35 for (File f : file.listFiles()) {
36 findFile(f);
37 }
38 } else {
39 count++;
40 String destFileName = destPath
41 + file.getAbsolutePath().substring(23);
42 System.out.println(destFileName);
43
44 FileReader r = new FileReader(file);
45 BufferedReader br = new BufferedReader(r);
46 String line = null;
47 BufferedWriter bw = null;
48 try {
49 FileWriter w = new FileWriter(destFileName);
50 bw = new BufferedWriter(w);
51
52 boolean isDocLine = false;
53 boolean isBlank = false;
54 while ((line = br.readLine()) != null) {
55
56 if (line.contains("//")){
57 line = line.substring(0, line.indexOf("//"));
58 }
59
60 if(isDocLine && !line.contains("*/")) continue;
61
62 if (line.contains("/*")) {
63 if (line.contains("*/")) {
64 line = line.substring(0, line.indexOf("/*"))
65 + line.substring(line.indexOf("*/") + 2);
66 } else {
67 isDocLine = true;
68 line = line.substring(0, line.indexOf("/*"));
69 }
70 }
71
72 if(line.contains("*/") && !line.contains("/*")){
73 line = line.substring(line.indexOf("*/") + 2);
74 isDocLine = false;
75 }
76
77 bw.write(line + "\r\n");
78 }
79 } catch (IOException e) {
80 e.printStackTrace();
81 } finally {
82 br.close();
83 bw.close();
84 }
85 }
86 }
87 }



posted @ 2012-04-08 00:09  海贼王路飞  阅读(346)  评论(0)    收藏  举报