大家面试的时候有没有被问到过这样一个问题:
你的代码量是多少?
WHAT???代码量???我怎么知道,难
道要我去数吗?
下面有简便方法实现:
方法一:
利用Eclipse开发工具可以简单的统计一个项目里面的代码行数
1、如图1,选中打开的一个项目右击,选中“search”选项,出现一个弹框;
2、在图2中的弹框中,输入如下所示内容,点击“search”,就会出现结果;
3、结果如图3:
方法二:
想要精确统计代码量,去掉空白行!去掉注释!没有问题啊,下面我们就用Java来实现吧,什么都不说了,直接上代码:
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public class CalculateRows { static long classcount = 0; // Java类的数量 static long normalLines = 0; // 空行 static long commentLines = 0; // 注释行 static long writeLines = 0; // 代码行 static long allLines = 0; // 代码行 public static void main(String[] args) throws Exception { File f = new File("D:/xampp/htdocs/Crazy"); // 目录 String type = ".java";//查找什么类型的代码,如".java"就是查找以java开发的代码量,".php"就是查找以PHP开发的代码量 CalculateRows.treeFile(f,type); System.out.println("路径:" + f.getPath()); System.out.println(type+"类数量:" + classcount); System.out.println("代码数量:" + writeLines); System.out.println("注释数量:" + commentLines); System.out.println("空行数量:" + normalLines); if(classcount==0){ System.out.println("代码平均数量:" + 0); }else{ System.out.println("代码平均数量:" + writeLines / classcount); } System.out.println("总 行数量:" + allLines); } /** * 查找出一个目录下所有的.java文件 * * @throws Exception */ public static void treeFile(File f,String type) throws Exception { File[] childs = f.listFiles(); for (int i = 0; i < childs.length; i++) { File file = childs[i]; if (!file.isDirectory()) { if (file.getName().endsWith(type)) { classcount++; BufferedReader br = null; boolean comment = false; br = new BufferedReader(new FileReader(file)); String line = ""; while ((line = br.readLine()) != null) { allLines++; line = line.trim(); if (line.matches("^[//s&&[^//n]]*$")) {//这一行匹配以空格开头,但不是以回车符开头,但以回车符结尾 normalLines++; } else if (line.startsWith("/*") && !line.endsWith("*/")) {//匹配以/*......*/括住的多行注释 commentLines++; comment = true; } else if (true == comment) { commentLines++; if (line.endsWith("*/")) { comment = false; }//匹配以//开头的单行注释,及以/*......*/括住的单行注释 } else if (line.startsWith("//") || (line.startsWith("/*")&&line.endsWith("*/"))) { commentLines++; } else {//其他的就是代码行 writeLines++; } } if (br != null) { br.close(); br = null; } } } else { treeFile(childs[i],type); } } } }
结果如下所示: