遍历处理文件夹下所有.java文件代码左侧含有形如 /* 56 */注释的代码,并存到新的目录下
package until;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* 程序会自动遍历所有文件夹下的.java文件并进行处理
* @author
*
*/
public class FileManager {
public static HashMap<Integer, String> map;
/**
* @param args
*/
public static void main(String[] args) {
/**
* 存放反编译好的代码
*/
File file = new File("D:/01/");
/**
* 程序自动创建并存放处理好的代码
*/
File file2 = new File("D:/02/");
getCode(file, file2);
}
//遍历dir目录下所有的.java文件名并处理格式注释的代码如右侧代码 /* 56 */ public void ....
//存储新的代码到newCodeDir下
public static void getCode(File dir, File newCodeDir) {
dir.mkdirs();
newCodeDir.mkdirs();
File[] files = dir.listFiles();
String cmd = "";
int index = 0;
String xmlPath = newCodeDir.getAbsolutePath() + "/";
xmlPath = xmlPath.replace("\\", "/");
newCodeDir.mkdirs();
Pattern p = Pattern.compile("/\\*[ \\d\\w]+\\*/");
for (int i = 0; i < files.length; i++) {
if (!files[i].isDirectory() && files[i].getName().endsWith(".java")) {
File file = files[i];
String path = file.getPath();
String name = file.getName();
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
DataOutputStream dos = null;
try {
fis = new FileInputStream(file);
isr = new InputStreamReader(fis, "UTF-8");
br = new BufferedReader(isr);
new File(xmlPath).mkdirs();
fos = new FileOutputStream(new File(xmlPath + name), true);
System.out.println(xmlPath + name);
dos = new DataOutputStream(fos);
String temp = null;
while ((temp = br.readLine()) != null) {
Matcher m = p.matcher(temp);
while (m.find()) {
String str = m.group();
temp = temp.replace(str, " ");
}
temp = temp + "\r\n";
dos.write(temp.getBytes("utf-8"));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dos != null) {
dos.close();
}
if (fos != null) {
fos.close();
}
if (br != null) {
br.close();
}
if (isr != null) {
isr.close();
}
if (fis != null) {
fis.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
} else if (files[i].isDirectory()) {
String path = newCodeDir.getAbsolutePath() + "/"
+ files[i].getName() + "/";
path = path.replace("\\", "/");
getCode(files[i], new File(path));
}
}
}
}