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++;
}
}
}
}
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++;
}
}
}
}
本文介绍了一个简单的Java程序,用于统计指定目录下所有Java文件中的空行、注释行及有效代码行数。通过遍历文件夹并解析每个.java文件的内容,程序能够准确地识别不同类型的代码行,并最终输出统计结果。
1018

被折叠的 条评论
为什么被折叠?



