日期:2014-05-20  浏览次数:20885 次

找到一个把指定目录下的java源码去掉注释放到另一目录下的一个java代码,但运行起来遇到读目录下加密过的文件出现错误...
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
  at java.util.Arrays.copyOf(Arrays.java:2882)
  at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.
java:100)
  at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:390
)
  at java.lang.StringBuffer.append(StringBuffer.java:224)
  at Dc2.profile(Dc2.java:37)
  at Dc2.prodir(Dc2.java:77)
  at Dc2.prodir(Dc2.java:74)
  at Dc2.dispReq(Dc2.java:87)
  at Dc2.main(Dc2.java:106)

遇到读*.gif,*.scr等文件也会破坏掉文件,所以要求跳过这两种类型文件和加密过的文件;


哪位能改进这段代码,使其能正常使用呢?
******************************************

/*
*把指定目录下的java源码去掉注释放到另一目录下
*作者:钝秀才 2005/7/14
*/

import java.io.*;

//把指定目录下的java源码去掉注释放到另一目录下
public class Dc2 {

static String srcStr = "E:\\upload";
static String desStr = "E:\\uploadre";

//处理文件
private void profile(File src, File des) throws Exception {

StringBuffer content = new StringBuffer();
StringBuffer output1 = new StringBuffer();
StringBuffer output2 = new StringBuffer();
String temp;
int start = 0;
int end = 0;
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");
}

//弃掉/* ... */注释,未考虑以//*开头的注释,
//且未考虑/*、*/等包含在""内的情形
//指定from以提高速度
while ( (start = content.indexOf("/*", start)) != -1) {
output1.append(content.substring(from, start));
from = content.indexOf("*/", start) + 2;
start = from;
}
output1.append(content.substring(from));

//弃掉// ... 注释
start = 0;
from = 0;
while ( (start = output1.indexOf("//", start)) != -1) {
output2.append(output1.substring(from, start));
from = output1.indexOf("\n", start); //保留回车换行
if (from == -1) { //不以"\n"结束
from = output1.length();
break;
}
start = from;
}
output2.append(output1.substring(from));

bw.write(output2.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) {

long starttime = System.currentTimeMillis();

if (args.length >= 2) {
srcStr = args[0];
desStr = args[1];
}
File src = new File(srcStr);
File des = new File(desStr);
try {
new Dc2().dispReq(src, des);
}
catch (Exception ex) {
System.out.println(ex);
}
long endtime = System.currentTimeMillis();
long usetime = endtime - starttime;
System.out.println("total use: " + usetime + " ms.");
}

}

------解决方案--------------------
或是写一个正则表达式可以批量去掉注释
,文件中有
***************************************
/* -------------------------------------

 (C) 2000-2006 .
 http://www.abc.com
------------------------------------- */