标签:use dem sys stringbu user comment lis nal 空白
因特殊业务需要,需要把源码中的注释全部去除
package com.example.demo.domain;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ClearComment {
private static int count = 0;
private static int num = 0;
public static void main(String[] args) throws Exception {
String rootDir = "/Users/userName/workspace/clweb";
deepDir(rootDir);
}
private static void deepDir(String rootDir) throws Exception {
File folder = new File(rootDir);
if (folder.isDirectory()) {
String[] files = folder.list();
if (files != null) {
for (String file1 : files) {
File file = new File(folder, file1);
if (file.isDirectory() && !file.isHidden()) {
deepDir(file.getPath());
} else if (file.isFile() && file.getName().endsWith(".jsx")) {
clearComment11(file.getPath());
}
}
}
} else if (folder.isFile() && folder.getName().endsWith(".jsx")) {
clearComment11(folder.getPath());
}
}
// private static void clearComment(String filePathAndName)
// throws Exception {
// System.out.println("-----开始处理文件:" + filePathAndName);
// num++;
// System.out.println("-----文件处理完成---" + num);
// //根据对应的编码格式读取
// BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePathAndName), "UTF-8"));
// StringBuffer content = new StringBuffer();
// String tmp = null;
//
// while ((tmp = reader.readLine()) != null) {
// content.append(tmp);
// content.append("\n");
// }
//
// String target = content.toString();
// //System.out.println(s);
// //使用对应的编码格式输出
// //String s = target.replaceAll("\\/\\/[^\\n]*|\\/\\*([^\\*^\\/]*|[\\*^\\/*]*|[^\\**\\/]*)*\\*\\/", ""); //本段正则摘自网上,有一种情况无法满足(/* ...**/),略作修改
// String s = target.replaceAll("\\/\\/[^\\n]*|\\/\\*([^\\*^\\/]*|[\\*^\\/*]*|[^\\**\\/]*)*\\*+\\/", "");
//
// BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(filePathAndName)), "UTF-8"));
// out.write(s);
// out.flush();
// out.close();
//// count++;
//// System.out.println("-----文件处理完成---" + count);
// }
private static void clearComment11(String filePathAndName)
throws FileNotFoundException {
System.out.println("-----开始处理文件:" + filePathAndName);
count++;
System.out.println("-----文件处理完成---" + count);
StringBuilder buffer = new StringBuilder();
String line = null; // 用来保存每行读取的内容
InputStream is = new FileInputStream(filePathAndName);
BufferedReader reader = new BufferedReader(new InputStreamReader(is,
StandardCharsets.UTF_8));
try {
line = reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append("\r\n"); // 添加换行符
try {
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
} // 读取下一行
}
// 1、清除单行的注释,如: //某某,正则为 :\/\/.*
// 2、清除单行的注释,如:/** 某某 */,正则为:\/\*\*.*\*\/
// 3、清除单行的注释,如:/* 某某 */,正则为:\/\*.*\*\/
// 4、清除多行的注释,如:
// /* 某某1
// 某某2
// */
// 正则为:.*/\*(.*)\*/.*
// 5、清除多行的注释,如:
// /** 某某1
// 某某2
// */
// 正则为:/\*\*(\s*\*\s*.*\s*?)*
String filecontent = buffer.toString();
Map<String, String> patterns = new HashMap<String, String>();
patterns.put("([^:])\\/\\/.*", "$1");// 匹配在非冒号后面的注释,此时就不到再遇到http://
patterns.put("\\s+\\/\\/.*", "");// 匹配“//”前是空白符的注释
patterns.put("^\\/\\/.*", "");
patterns.put("^\\/\\*\\*.*\\*\\/$", "");
patterns.put("\\/\\*.*\\*\\/", "");
patterns.put("/\\*(\\s*\\*\\s*.*\\s*?)*\\*\\/", "");
//patterns.put("/\\*(\\s*\\*?\\s*.*\\s*?)*", "");
Iterator<String> keys = patterns.keySet().iterator();
String key, value;
while (keys.hasNext()) {
// 经过多次替换
key = keys.next();
value = patterns.get(key);
filecontent = replaceAll(filecontent, key, value);
}
// 再输出到原文件
try {
File f = new File(filePathAndName);
if (!f.getParentFile().exists()) {
final boolean mkdirs = f.getParentFile().mkdirs();
}
FileOutputStream out = new FileOutputStream(filePathAndName);
byte[] bytes = filecontent.getBytes(StandardCharsets.UTF_8);
out.write(bytes);
out.flush();
out.close();
count++;
System.out.println("-----文件处理完成---" + count);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param fileContent 内容
* @param patternString 匹配的正则表达式
* @param replace 替换的内容
*/
public static String replaceAll(String fileContent, String patternString, String replace) {
String str = "";
Matcher m;
Pattern p;
try {
p = Pattern.compile(patternString);
m = p.matcher(fileContent);
str = m.replaceAll(replace);
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
注意事项:有些注释不规范,在删除代码过程中,可能会删除多余代码,也可能会出现删除部分注释的问题,需要重启项目,让项目代码正常run起来
标签:use dem sys stringbu user comment lis nal 空白
原文地址:https://www.cnblogs.com/cloudwas/p/13159072.html