删除Java代码中的所有注释

 
 
文章来源:http://blog.csdn.net/fullstack/article/details/22688777
package tools ;

import java.io.BufferedReader ;
import java.io.BufferedWriter ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.FileOutputStream ;
import java.io.InputStreamReader ;
import java.io.OutputStreamWriter ;

/**
* 删除Java代码中的注释
*
* @author Alive
* @build 2010-12-23
*/
public class DeleteComments {

private static int count = 0 ;

/**
* 删除文件中的各种注释,包含//、/* * /等
* @param charset 文件编码
* @param file 文件
*/
public static void clearComment ( File file , String charset ) {
try {
//递归处理文件夹
if (! file . exists ()) {
return ;
}

if ( file . isDirectory ()) {
File [] files = file . listFiles ();
for ( File f : files ) {
clearComment ( f , charset ); //递归调用
}
return ;
} else if (! file . getName (). endsWith ( ".java" )) {
//非java文件直接返回
return ;
}
System . out . println ( "-----开始处理文件:" + file . getAbsolutePath ());

//根据对应的编码格式读取
BufferedReader reader = new BufferedReader ( new InputStreamReader ( new FileInputStream ( file ), charset ));
StringBuffer content = new StringBuffer ();
String tmp = null ;
while (( tmp = reader . readLine ()) != null ) {
content . append ( tmp );
content . append ( "\n" );
}
String target = content . toString ();
//String s = target.replaceAll("\\/\\/[^\\n]*|\\/\\*([^\\*^\\/]*|[\\*^\\/*]*|[^\\**\\/]*)*\\*\\/", ""); //本段正则摘自网上,有一种情况无法满足(/* ...**/),略作修改
String s = target . replaceAll ( "\\/\\/[^\\n]*|\\/\\*([^\\*^\\/]*|[\\*^\\/*]*|[^\\**\\/]*)*\\*+\\/" , "" );
//System.out.println(s);
//使用对应的编码格式输出
BufferedWriter out = new BufferedWriter ( new OutputStreamWriter ( new FileOutputStream ( file ), charset ));
out . write ( s );
out . flush ();
out . close ();
count ++;
System . out . println ( "-----文件处理完成---" + count );
} catch ( Exception e ) {
e . printStackTrace ();
}
}

public static void clearComment ( String filePath , String charset ) {
clearComment ( new File ( filePath ), charset );
}

public static void clearComment ( String filePath ) {
clearComment ( new File ( filePath ), "UTF-8" );
}

public static void clearComment ( File file ) {
clearComment ( file , "UTF-8" );
}

public static void main ( String [] args ) {
clearComment ( "D:\\eclipse3.3\\workspace\\Sanguosha" ); //删除目录下所有java文件注释
//删除某个具体文件的注释
//clearComment("D:\\proj\\scm\\action\\AbcdefgAction.java");
}

}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个简单的Java程序,用于删除Java代码的所有注释: ``` import java.io.*; public class RemoveComments { public static void main(String[] args) throws IOException { String inputFileName = "inputFile.java"; String outputFileName = "outputFile.java"; BufferedReader reader = new BufferedReader(new FileReader(inputFileName)); BufferedWriter writer = new BufferedWriter(new FileWriter(outputFileName)); String line = reader.readLine(); boolean inBlockComment = false; while (line != null) { if (!inBlockComment) { line = removeLineComments(line); } if (line.contains("/*")) { inBlockComment = true; line = removeBlockCommentStart(line); } if (inBlockComment) { if (line.contains("*/")) { inBlockComment = false; line = removeBlockCommentEnd(line); } else { line = ""; } } writer.write(line); writer.newLine(); line = reader.readLine(); } reader.close(); writer.close(); } private static String removeLineComments(String line) { int index = line.indexOf("//"); if (index != -1) { line = line.substring(0, index); } return line; } private static String removeBlockCommentStart(String line) { int index = line.indexOf("/*"); if (index != -1) { line = line.substring(0, index); } return line; } private static String removeBlockCommentEnd(String line) { int index = line.indexOf("*/"); if (index != -1) { line = line.substring(index + 2); } return line; } } ``` 该程序将从名为“inputFile.java”的文件读取Java代码,并将删除所有注释后将结果写入名为“outputFile.java”的文件。 该程序使用三个帮助方法来删除不同类型的注释: - `removeLineComments`方法删除单行注释(以“//”开头的注释)。 - `removeBlockCommentStart`方法删除注释的开始部分(以“/*”开头的注释)。 - `removeBlockCommentEnd`方法删除注释的结束部分(以“*/”结尾的注释)。 该程序还使用一个布尔变量`inBlockComment`来跟踪是否当前正在处理块注释。如果是,则程序将跳过所有行,直到找到块注释的结尾。否则,程序将删除单行注释并处理块注释的开始。 请注意,该程序仅删除Java代码注释,并且可能无法正确处理某些情况,例如在字符串或字符字面量注释。因此,请谨慎使用。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值