import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class MainClass {
static long normalline = 0;
static long commetline = 0;
static long writeline = 0;
public static void main(String[] args) {
File file = new File("D:\\java_projects\\MainTestClass\\src");
File[] allfiles = file.listFiles();
for (File child : allfiles) {
if (child.getName().matches(".*\\.java$")) {
parse(child);
}
}
System.out.println("normalline="+normalline);
System.out.println("commentline="+commetline);
System.out.println("whiteline="+writeline);
}
/* zhu shi */
/* * zhu shi */
private static void parse(File child) {
boolean comment=false;
BufferedReader bufferedReader=null;
try {
bufferedReader = new BufferedReader(new FileReader(child));
String line="";
while ((line=bufferedReader.readLine())!=null) {
line = line.trim();
if (line.matches("^[\\s&&[^\\n]]*$")) {
writeline++;
}else if (line.startsWith("/*") && !line.endsWith("*/")) {
commetline++;
comment=true;
}else if (comment==true) {
commetline++;
if (line.endsWith("*/")) {
comment=false;
}
}else if (line.startsWith("//")) {
commetline++;
}else if (line.startsWith("/*")&&line.endsWith("*/")) {
commetline++;
}else {
normalline++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
output:
normalline=54
commentline=4
whiteline=4