java 统计代码量

6 篇文章 0 订阅


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:/gitxxxx/student-service/src/main/java"); // 目录
        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);
        }
    }
}

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值