用正则表达式统计java工作行,注释行,空白行

来源:互联网 发布:java开发app 编辑:程序博客网 时间:2024/04/27 09:44
static long blankline=0;
static long commentline=0;
static long workline=0;

public static void main(String[] args) throws IOException {
File file = new File("d:\\JAVA\\day12\\");
File[] listFiles = file.listFiles();
for (File file2 : listFiles) {
if(file2.getName().matches(".*\\.java$")){
Parser(file2);
}
}

System.out.println("空行:"+blankline);
System.out.println("注释行:"+commentline);
System.out.println("工作行:"+workline);
}


private static void Parser(File file2) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file2));
String line="";
boolean comment=false;
while((line=reader.readLine()) != null){
line=line.trim();
if(line.matches("^[\\s&&[^\\n]]*$")){
blankline++;
}else if(line.startsWith("/*") && !line.endsWith("*/")){
commentline++;
comment=true;


}else if(true==comment){
commentline++;
if(line.endsWith("*/")){
comment=false;
}
}else if(line.startsWith("//")){
commentline++;
}else if(line.startsWith("/*")&& line.endsWith("*/")){
commentline++;
}else{
workline++;
}


}

}
}
0 0
原创粉丝点击