package regex;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CountCode {
private static int codeNum=0;
private static int bankNum=0;
private static int comentNum=0;
private static boolean b=false;
public static void main(String[] args) {
try {
BufferedReader br=new BufferedReader(new FileReader("F:\\java project\\TankWar8\\src\\Tank.java"));
String code="";
while((code=br.readLine())!=null){
parse(code);
}
System.out.println("有效代码行数:"+codeNum);
System.out.println("空白行数:"+bankNum);
System.out.println("注释行数:"+comentNum);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void parse(String code) {
if(code.matches("^\\s*$")){
bankNum++;
}/*else if(code.trim().matches("(?!import|package).+;\\s*(((//)|(/\\*+)).*)*")){ //包括注释的代码行数(不准确)
codeNum++;
}*/else if(code.trim().startsWith("/*") && !code.trim().endsWith("*/")){
comentNum++;
b=true;
}else if(b==true){
comentNum++;
if(code.trim().endsWith("*/")){
b=false;
}
}else if(code.trim().startsWith("/*") && code.trim().endsWith("*/")){
comentNum++;
}else if(code.trim().startsWith("//")){
comentNum++;
}else{
codeNum++;
}
}
}
|