项目做到现在突然想从一个侧面来反映自己的劳动成果——那就是代码行数。 在网上找统计代码行数的工具,找了好几个不但不可以用而且还携带了病毒... ... 于是自己动手写了一个, 是单个程序版的,有待日后完善... ... ^_^ ^_^
package schoolFellow;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class JavaCodeCount {
private static int normallines = 0;
private static int commentlines = 0;
private static int spacelines = 0;
public static void main(String args[]) {
JavaCodeCount test = new JavaCodeCount();
File file = new File("D://MyEclipse2//My_MVC//Dog//src");// 引号中输入路径
test.list(file);
System.out.println("normallines: " + normallines);
System.out.println("commentlines: " + commentlines);
System.out.println("spacelines: " + spacelines);
}
public void list(File file) {
if(file.isDirectory()) {
File[] childs = file.listFiles();
for(File temp:childs) {
list(temp);
}
} else if(file.getName().matches(".+java$")) {
count(file);
}
}
private void count(File file) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String str;
boolean flag = false;
while((str = br.readLine()) != null) {
str = str.trim();
if(str.matches("//s*$")) {
spacelines ++;
} else if(str.startsWith("//")) {
commentlines ++;
} else if(str.startsWith("/*") && str.endsWith("*/")) {
commentlines ++;
} else if(str.startsWith("/*")) {
commentlines ++;
flag = true;
} else if(flag == true) {
commentlines ++;
if(str.endsWith("*/")) {
flag = false;
}
} else {
normallines ++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}