package com.lookcoder.inschool.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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; /**
* title: 清除注释
*/
public class ClearComment { public static void main(String[] args) throws FileNotFoundException {
String rootDir = "E:\\文档\\SpringWebMvcSource\\spring-webmvc-4.3.9.RELEASE-sources";
deepDir(rootDir);
} private static void deepDir(String rootDir) throws FileNotFoundException {
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(".java")) {
clearComment(file.getPath());
}
}
}
} else if (folder.isFile() && folder.getName().endsWith(".java")) {
clearComment(folder.getPath());
}
} private static void clearComment(String filePathAndName)
throws FileNotFoundException {
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();
} 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;
}
}

最新文章

  1. Tables without a clustered index are not supported in this version of SQL Server. Please create a clustered index and try again.
  2. [LintCode] Segment Tree Build 建立线段树
  3. 将CentOS配置成本地yum
  4. 人工智能起步-反向回馈神经网路算法(BP算法)
  5. HDU 5296 Annoying problem (LCA,变形)
  6. iOS--消息推送后方法回调情况(简)
  7. 如何写angularJS模块
  8. .Net程序员 Solr-5.3之旅 (三)Solr 从MSSQ导入索引数据
  9. jquery使用load开展局部刷新没有效果
  10. CodeForces 698A - Vacations (Codeforces Round #363 (Div. 2))
  11. POSIX 消息队列相关问题
  12. SQLServer导出数据到MySQL
  13. MySQL(InnoDB)是如何处理死锁的
  14. [Swift]LeetCode186. 翻转字符串中的单词 II $ Reverse Words in a String II
  15. MySQL面试题中:主从同步部署介绍
  16. angular,vue,react的父子通信
  17. java.lang.RuntimeException: wrong class format Caused by: org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException: null
  18. cocos2dx为Sprite添加触摸事件监听器
  19. 9.3AspectJ
  20. [UOJ55]紫荆花之恋

热门文章

  1. JSP内置对象(下)
  2. Docke 镜像加速
  3. 爬虫之scrapy框架的crawlspider
  4. 使用kubeadm部署k8s
  5. Hbase架构与原理(转)
  6. python dijkstra 最短路算法示意代码
  7. The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple (Mirror)
  8. JS关闭当前窗口
  9. ES6新特性之 let 、const
  10. Fiddler——手机端无法安装证书