public class CodeCounter {
static int whiteLine = 0;
static int codeLine = 0;
static int commentLine = 0;
public static void main(String[] args) {
parse();
System.out.println(whiteLine);
System.out.println(codeLine);
System.out.println(commentLine);
}
private static void parse() {
//Pattern p = Pattern.compile("[\\w-]+@[\\w[-.]]+\\.[\\w]+");
Pattern p = Pattern.compile("^[\\s&&[^\\n]]*$");
BufferedReader br = null;
String line = null;
boolean comment = false;
try {
br = new BufferedReader(new FileReader("F:\\download\\Google\\csdn.html"));
while((line = br.readLine()) != null) {
line.trim();
Matcher m = p.matcher(line);
if(m.find()){
whiteLine ++;
} else if(line.startsWith("/*") && !line.endsWith("*/")) {
comment = true;
commentLine ++;
} else if (true == comment) {
commentLine ++;
if(line.endsWith("*/")){
comment = false;
}
} else if (line.startsWith("/*") && line.endsWith("*/")) {
commentLine ++;
} else {
codeLine ++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
TestCodeRowCount_Demo
最新推荐文章于 2023-05-10 08:59:41 发布