求一个JAVA文件里面的代码行空行和注释行

2025-06-23 01:00:08
推荐回答(1个)
回答1:

package demo1;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * @ClassName: BufferedDemo
 * @Description: (获得Java文件中的空行和注释行,作为工具类)
 * @author  幸福源于点滴珍惜!
 * @author 砖尘
 * @date Mar 18, 2014 8:55:18 PM
 * 
 */
public class BufferedDemo {
private String path;
private BufferedReader bfr;
private int count;

public BufferedDemo(String path) throws FileNotFoundException {
this.path = path;
bfr = new BufferedReader(new FileReader(path));
}

public int getNullLine() throws IOException {// 获得空行
String len = "";
while ((len = bfr.readLine()) != null) {
if (len.equals(""))
count++;
}
return count;
}

public int getNotesLine() throws IOException {// 获取注释行,这边仅认为/***/
// 和/**/是注释。
String len = "";
while ((len = bfr.readLine()) != null) {
if (len.contains("/*")) {
if (len.contains("*/"))
count++;
else {
count += 2;
while (!(len = bfr.readLine()).contains("*/"))
count++;
}

}
}
return count;
}
}

 工具类中没有写主函数,要运行需要增加主函数。