请教代码注释的问题(Java语言环境下):
1.如何用程序的方式去除一个文件中的所有注释?
2.当在源代码中匹配一个字符串时,如何判断它是否已被注释? 如/* this.setName("name"); */
当我匹配“Name”字符串判断那一行是不是被注释掉?不知道有何解决办法?每个问题50分,最好有相关代码呃,3Q!
1.如何用程序的方式去除一个文件中的所有注释?
2.当在源代码中匹配一个字符串时,如何判断它是否已被注释? 如/* this.setName("name"); */
当我匹配“Name”字符串判断那一行是不是被注释掉?不知道有何解决办法?每个问题50分,最好有相关代码呃,3Q!
解决方案 »
- 我做了个BBS,高手过来指点指点~~
- 求教高手----关于文件打开的操作
- (初学者)java连SQL2000中数据类型问题
- 寻求JAVA高等数学公式算法
- method <init>(Ljava/lang/String;)V not found是怎樣的錯誤?
- 这里的FAQ好乱,只好发新帖提问!
- 有没有谁知道短信网关接口协议CMPP和SGIP的JAVA API的的下载,急需!
- 最近碰到一怪事~applet在某些机子上不能正常显示,help~~~~
- socket使用两个线程分别收发,收到结束标识怎么结束发线程的流
- 哪位大虾对WebPhone有所了解?
- 请问JDK6.0和tomcat6.0兼容吗?
- while(true)如何退出循环?
应该可以实现了吧
str.replace("//", " ")
str.replace("/*","")
str.repalec("*/","")
/*
the first line
the second line
*/the middle statements are not what I need.
Maybe the 反射或者正则表达式 is ok. But how can I implement it? Some examples are better!
Many thanks!
package color;/**
* My firat Java program: Hello World
* @author bao110908
*/
public class Test{ /*
main method
method of automatic startup
*/
/* abnormal comment 1 */
/** abnormal comment 2 **/
// normal comment
public static void main(String[] args) {
System.out.println("Hello World"); // print 'Hello World'
System.out.println("Hello World"); /* abnormal comment 3 */
}
}
代码:import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;public class Test {
public static void main(String[] args) {
Comment comment = new Comment();
int count = comment.countCommentLine("abc.java");
System.out.println("总共的注释行数:" + count);
System.out.println();
List<String> codes = comment.removeComment("abc.java");
System.out.println("没有注释的源代码");
for(String str : codes) {
System.out.println(str);
}
}
}class Comment {
/**
* 计算某个 Java 文件中注释的行数
*/
public int countCommentLine(String filename) {
if(filename.toLowerCase().lastIndexOf(".java") <= 0) {
throw new IllegalJavaFile(filename);
}
int count = 0;
List<String> list = Util.readFile(filename);
boolean isMultiLineComment = false;
for(String str : list) {
// 当为多行注释时,直接将计算注释行
if(isMultiLineComment) {
count++;
// 直到碰到 */ 为止,当然 */ 这一行也算注释
if(str.indexOf("*/") >= 0) {
isMultiLineComment = false;
}
}
// 为 // 直接计数
if(str.indexOf("//") >= 0) {
count++;
}
// 当碰到 /* 时,开始计数
if(str.indexOf("/*") >= 0) {
count++;
// 并且在该行中没 */ 时,可以看作是多行注释的开始
if(str.indexOf("*/") < 0) {
isMultiLineComment = true;
}
}
}
return count;
}
/**
* 除去文件中的注释,与 countCommentLine 方法类似
*/
public List<String> removeComment(String filename) {
if(filename.toLowerCase().lastIndexOf(".java") <= 0) {
throw new IllegalJavaFile(filename);
}
List<String> list = Util.readFile(filename);
List<String> codes = new ArrayList<String>();
boolean isMultiLineComment = false;
for(int i = 0, k = list.size(); i < k; i++) {
String code = list.get(i);
if(isMultiLineComment) {
if(code.indexOf("*/") >= 0) {
isMultiLineComment = false;
}
continue;
}
int index = code.indexOf("/*");
if(index >= 0) {
String s = code.substring(0, index);
if(s.trim().length() > 0) {
codes.add(s);
}
if(code.indexOf("*/") < 0) {
isMultiLineComment = true;
}
continue;
}
index = code.indexOf("//");
if(index >= 0) {
String s = code.substring(0, index);
if(s.trim().length() > 0) {
codes.add(s);
}
continue;
}
codes.add(code);
}
return codes;
}
}class IllegalJavaFile extends RuntimeException {
private static final long serialVersionUID = 1L;
public IllegalJavaFile(String filename){
super("\"" + filename + "\", the file is not a java file.");
}
}class Util {
public static List<String> readFile(String filename) {
List<String> list = new ArrayList<String>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filename));
String str = "";
while((str = br.readLine()) != null) {
// 忽略空行
if(str.trim().length() > 0) {
list.add(str);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
public static void writeFile(List<String> codes, String filename) {
BufferedWriter br = null;
try {
br = new BufferedWriter(new FileWriter(filename));
String line = System.getProperty("line.separator");
for(int i = 0, k = codes.size(); i < k; i++) {
br.write(codes.get(i));
br.write(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 判断 关键字 所在行 是否被注释掉
public List<String> isComment(String filename, String keyword) {
if (filename.toLowerCase().lastIndexOf(".java") <= 0) {
throw new IllegalJavaFile(filename);
}
List<String> list = Util.readFile(filename,false);
List<String> flag = new ArrayList<String>();
boolean isMultiLineComment = false;
for (int i = 0, k = list.size(); i < k; i++) {
String code = list.get(i); if (isMultiLineComment) {
if (code.indexOf(keyword) >= 0)
flag.add("Line " + (i + 1) + " : " + keyword
+ " is comment");
if (code.indexOf("*/") >= 0) {
isMultiLineComment = false;
}
continue;
} int index = code.indexOf("/*");
if (index >= 0) {
if (code.indexOf(keyword) >= 0)
flag.add("Line " + (i + 1) + " : " + keyword
+ " is comment");
if (code.indexOf("*/") < 0) {
isMultiLineComment = true;
}
continue;
} index = code.indexOf("//");
if (index >= 0) {
if (code.indexOf(keyword) >= 0)
flag.add("Line " + (i + 1) + " : " + keyword
+ " is comment");
continue;
}
}
return flag;
}
Many thanks to bao110908 and I have added new function about how to judge the line which has the keyword is commented or not.Another question is how can I compare the two xml files and calculate the delta-values of the nodes in those two xml files. Any help is welcome!The samle xml file:
XML1<project name="bean1.lst">
<package name="bean">
<type kind="class" name="Demo">
<metric name="LOCC" value="54"/>
<metric name="DIT" value="0"/>
<metric name="NOC" value="0"/>
<metric name="CFA" value="0"/>
<metric name="CMC" value="1"/>
<metric name="CBM" value="1"/>
<metric name="CDA" value="0"/>
<metric name="CDP" value="0"/>
<metric name="CDI" value="0"/>
<metric name="CAE" value="0"/>
<metric name="CAM" value="0"/>
<metric name="CAA" value="0"/>
<metric name="CAI" value="0"/>
<metric name="CIM" value="0"/>
<metric name="RFA" value="9"/>
<metric name="RFM" value="9"/>
<metric name="RFP" value="0"/>
</type>
<type kind="class" name="Point">
<metric name="LOCC" value="46"/>
<metric name="DIT" value="0"/>
<metric name="NOC" value="0"/>
<metric name="CFA" value="0"/>
<metric name="CMC" value="0"/>
<metric name="CBM" value="0"/>
<metric name="CDA" value="0"/>
<metric name="CDP" value="0"/>
<metric name="CDI" value="0"/>
<metric name="CAE" value="0"/>
<metric name="CAM" value="0"/>
<metric name="CAA" value="0"/>
<metric name="CAI" value="0"/>
<metric name="CIM" value="0"/>
<metric name="RFA" value="12"/>
<metric name="RFM" value="12"/>
<metric name="RFP" value="0"/>
</type>
</package>
</project>
XML2
<project name="bean2.lst">
<package name="bean">
<type kind="class" name="Demo">
<metric name="LOCC" value="54"/>
<metric name="DIT" value="0"/>
<metric name="NOC" value="0"/>
<metric name="CFA" value="0"/>
<metric name="CMC" value="1"/>
<metric name="CBM" value="1"/>
<metric name="CDA" value="0"/>
<metric name="CDP" value="0"/>
<metric name="CDI" value="0"/>
<metric name="CAE" value="1"/>
<metric name="CAM" value="0"/>
<metric name="CAA" value="1"/>
<metric name="CAI" value="0"/>
<metric name="CIM" value="0"/>
<metric name="RFA" value="10"/>
<metric name="RFM" value="9"/>
<metric name="RFP" value="1"/>
</type>
<type kind="class" name="Point">
<metric name="LOCC" value="26"/>
<metric name="DIT" value="0"/>
<metric name="NOC" value="0"/>
<metric name="CFA" value="0"/>
<metric name="CMC" value="0"/>
<metric name="CBM" value="0"/>
<metric name="CDA" value="0"/>
<metric name="CDP" value="0"/>
<metric name="CDI" value="0"/>
<metric name="CAE" value="1"/>
<metric name="CAM" value="0"/>
<metric name="CAA" value="1"/>
<metric name="CAI" value="1"/>
<metric name="CIM" value="0"/>
<metric name="RFA" value="8"/>
<metric name="RFM" value="7"/>
<metric name="RFP" value="1"/>
</type>
<type kind="aspect" name="BoundPoint"> // Be careful, this is the additional content!!!
<metric name="LOCC" value="37"/>
<metric name="DIT" value="0"/>
<metric name="NOC" value="0"/>
<metric name="CFA" value="1"/>
<metric name="CMC" value="3"/>
<metric name="CBM" value="3"/>
<metric name="CDA" value="2"/>
<metric name="CDP" value="2"/>
<metric name="CDI" value="1"/>
<metric name="CAE" value="0"/>
<metric name="CAM" value="0"/>
<metric name="CAA" value="0"/>
<metric name="CAI" value="0"/>
<metric name="CIM" value="2"/>
<metric name="RFA" value="6"/>
<metric name="RFM" value="6"/>
<metric name="RFP" value="0"/>
</type>
</package>
</project>And what I want to get is the delta-value about the two xml files according to the node(package,name).The similar code maybe better! 3Q~
public void SplitCodeComment(LineNumberReader p_Reader, PrintStream p_CodeOutput, PrintStream p_Comment){
while(true){
try{
String _Line = p_Reader.readLine();
if(_Line == null)break;
boolean _Muti = false;
if(_Line.startsWith("//")){
p_Comment.println(_Line);
continue;
}
else if(_Line.startsWith("/*")){
if(!_Line.endsWith("*/"))_Muti = true;
p_Comment.println(_Line);
continue;
}else if(_Line.endsWith("*/")){
_Muti = false;
p_Comment.println(_Line);
continue;
}
if(_Muti)
p_Comment.println(_Line);
else
p_CodeOutput.println(_Line);
}catch(Exception ex){
ex.printStackTrace();
break;
}
}