源码研究小工具--删除代码注释
dxcai 2005-07-13 09:37:02 一个去除java源码注释的小工具
我们在研究java API的源码时,有时会觉得注释太多了(大部分代码都是浅显易懂的),往往比代码本身
还多(sun做得真够体贴入微的),以至我们不得不在大块大块的注释中寻找代码。
于是我们就会想:要是有一个无注释的版本就好了,一个class的代码都可以一览无余,省却许多注释来障
目。当确实看不懂时才看注释版本。
这就是小弟花点时间做这个class的意义。下边把代码贴出来,以方便大家。
运行时要指定两个参数:输入目录和输出目录(单个文件也可以)
/*
*把指定目录下的java源码去掉注释放到另一目录下
*作者:钝秀才 2005/7/13
*/
import java.io.*;
public class Dc {
static String srcStr = "E:\\j2seAPIsrc";
static String desStr = "E:\\j2seAPIsrc_无注释";
//处理文件
private void profile(File src, File des) throws Exception {
StringBuffer content = new StringBuffer();
String temp;
int start;
int end;
int from = 0;
BufferedReader br = new BufferedReader(new FileReader(src));
BufferedWriter bw = new BufferedWriter(new FileWriter(des));
while ( (temp = br.readLine()) != null) {
content.append(temp);
content.append("\n");
}
while ( (start = content.indexOf("/*", from)) != -1) {
end = content.indexOf("*/", start) + 2;
content.delete(start, end);
from = start;
}
from = 0;
int e;
//指定from以提高速度
while ( (start = content.indexOf("//", from)) != -1) {
e = content.indexOf("\n", start);
end = (e == -1) ? content.length() : (e + 1);
content.delete(start, end);
from = start;
}
bw.write(content.toString());
br.close();
bw.close();
}
//处理目录
private void prodir(File src, File des) throws Exception {
des.mkdir(); //创建目录
File[] files = src.listFiles();
for (int i = 0; i < files.length; i++) {
//下一级目录/文件
File nf = new File(des, files[i].getName());
if (files[i].isDirectory()) {
//是目录,递归
prodir(files[i], nf);
}
else { //文件
profile(files[i], nf);
}
}
}
//输入目录还是文件
public void dispReq(File src, File des) throws Exception {
if (src.isDirectory()) {
prodir(src, des);
}
else {
profile(src, des);
}
}
public static void main(String[] args) {
if (args.length >= 2) {
srcStr = args[0];
desStr = args[1];
}
File src = new File(srcStr);
File des = new File(desStr);
try {
new Dc().dispReq(src, des);
}
catch (Exception ex) {
System.out.println(ex);
}
}
}