用这个程序测代码行、空行和注释行,老是显示都为0行,麻烦看看哪里错了啊
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class CodeCounter {
static long codeLines = 0;
static long commentLines = 0;
static long whiteLines = 0;
public static void main(String[] args) {
File f = new File("D:\\Workspaces\\MyEclipse Professional 2014\\DOMTest\\src\\question");
File [] codeFiles = f.listFiles();
for (File file : codeFiles) {
if (file.getName().matches(".* \\.java$")) {
parse(file);
}
}
System.out.println(codeLines);
System.out.println(commentLines);
System.out.println(whiteLines);
}
private static void parse(File f) {
BufferedReader br = null;
boolean comment = false;
try {
br = new BufferedReader(new FileReader(f));
String line = "";
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.matches("^[\\s&&[^\\n]]*$")) {
whiteLines++;
} else if (line.startsWith("/*") && line.endsWith("*/")) {
commentLines++;
comment = true;
} else if (true == comment) {
commentLines ++;
if (line.endsWith("*/")) {
comment = false;
}
} else {
codeLines ++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
相关分类