67,519
社区成员
发帖
与我相关
我的任务
分享
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("CPTest1.java")));
String buf;
StringBuilder content = new StringBuilder();
while((buf=br.readLine())!=null) {
content.append(buf).append("\n"); //把文件所有的内容保存到一个content中
}
br.close();
String text = content.toString();
text = text.replaceAll("[/][*](.*?\\s*)*(.*?|\\s*)[*][/]", ""); //替换/**/之间的内容
text = text.replaceAll("//.*?(\n)+", "\n"); //替换//的注释,如果删除后面的空白行,则(\n)+,否则\n就可以了
System.out.println(text);
String s = "hafdsdgdfgfdgg;\n/*fdadasfsd\nfdasfdsf*/\nfasfjsdf;"; //so on
String result = s.replaceAll("[/][*](.*?\\s+)*(.*?|\\s+)[*][/]", "");
System.out.println(result);