项目完成了,现在经理想做个统计,就是说统计项目中一共有多少的配置文件,类,JSP页面。还有多少的代码,请问这个有什么方法实现么?

解决方案 »

  1.   

    贴个代码,是统计项目java代码量的,其他的可以相同的思路吧package util;import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;public class CodeCounter { //有效行数
    private static long normalLines = 0;

    //注释行数
    private static long commentLines = 0;

    //空白行
    private static long whiteLines = 0; private ArrayList<File> al = new ArrayList<File>(); /**
     * 递归找到该目录下的所有java源文件
     * @param homeDir
     */
    public void findJavaSource(File homeDir) {
    if(homeDir.isFile()){
    al.add(homeDir);
    return;
    }
    File[] f = homeDir.listFiles();
    for (File child : f) {
    if (child.isFile() && child.getName().matches(".*\\.java$")) {
    al.add(child);
    } else if (child.isDirectory()) {
    findJavaSource(child);
    }
    }
    } public static void main(String []str){
    CodeCounter cc = new CodeCounter();
    //测试时别忘了该目录地址
    File homeDir = new File("E:\\workspace_eclipse_3.3.2\\GCSMS1.0\\src");
    cc.count(homeDir);
    }

    public void count(File homeDir) {
    this.findJavaSource(homeDir);
    for (File child : al) {
    parse(child);
    }
    System.out.println("normalLines:" + normalLines);
    System.out.println("commentLines:" + commentLines);
    System.out.println("whiteLines:" + 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 (line.startsWith("/*") && line.endsWith("*/")) {
    commentLines++;
    } else if (true == comment) {
    commentLines++;
    if (line.endsWith("*/")) {
    comment = false;
    }
    } else if (line.startsWith("//")) {
    commentLines++;
    } else {
    normalLines++;
    }
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (br != null) {
    try {
    br.close();
    br = null;
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }}
      

  2.   

    1楼的,你的 for (File child : f) {只能是在JDK1.5下运行,
    我的是JDK1.4郁闷
      

  3.   


    有工具,可以统计代码的行数,包括jsp和java文件,以前用过,忘了叫什么名字啦,回头我给你找一下吧
      

  4.   

    I am trying to add an UI, maybe in two days...