62,612
社区成员
发帖
与我相关
我的任务
分享
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
//代码测试工具(开正经代码行数,注释行数,空白行数)
//测试最近正则表达式Demo的代码情况
public class Demo10 {
static int i, j, k;
public static void main(String[] args) {
File file = new File("F:\\java测试用例\\Test0\\src\\");
String[] buff = file.list();
for (String filename : buff) {
Code(filename);
}
System.out.println("空白行有:" + i + "行");
System.out.println("注释行有:" + j + "行");
System.out.println("代码行有:" + k + "行");
}
public static void Code(String string) {
boolean a = false;// 多行注释的标志位,如果匹配到/* 则置为true;然后继续读取行,如果匹配到*/ 则置为false 跳出循环(详情见下面代码)
String string1 = null;
// 读取磁盘文件内容
try {
BufferedReader stream0 = new BufferedReader(new FileReader("F:\\java测试用例\\Test0\\src\\" + string));
String string2 = stream0.readLine();
while (string2 != null) {
string1 = string2.concat("\r\n");// 解决readLine()不返回回车换行符的问题
// 空白行
if ((string1.matches("^\\s*\\n$")) == true) {
i++;
}
// 单行注释
else if ((string2.matches("\\s*//.*")) == true) {
j++;
}
// 多行注释
else if ((string2.matches("\\s*/\\*.*")) == true) {
a = true;
j++;
while (a == true) {
string2 = stream0.readLine();
if ((string2.matches("\\s*.*\\*/")) == true) {
a = false;
j++;
} else {
j++;
}
}
// 代码行
} else {
k++;
}
string2 = stream0.readLine();
}
// 关闭流
stream0.close();
} catch (FileNotFoundException e) {
System.out.println("没有找到文件");
e.printStackTrace();
} catch (IOException e) {
System.out.println("流读取失败");
e.printStackTrace();
}
}
}
String path = "F:\\java测试用例\\Test0\\src\\"
File file = new File(path, string);
if(file.isDirectory()){
return;
}