java统计代码的行数

java统计代码的行数,区别空行、注释行、代码行、配置行

package com.lxh.config.utils;

import java.io.*;

/**
 * @ClassName: CountCodeLines
 * @Author: lxh
 * @Description: 计算代码的行数
 * @Date: 2022/8/1 14:53
 */
public class CountCodeLines {
    /** 空行 */
    private static long nullLines = 0;
    /** 注释行 */
    private static long annoLines = 0;
    /** 代码行 */
    private static long codeLines = 0;
    /** 配置文件行 */
    private static long configLines = 0;
    /** 总行 */
    private static long allLines = 0;

    public static void main(String[] args) {
        CountCodeLines ccl = new CountCodeLines();
        ccl.listFile("D:\\Java\\gitCode\\fire-command-platform-code");
        System.out.println("空行:" + nullLines);
        System.out.println("注释行:" + annoLines);
        System.out.println("代码行:" + codeLines);
        System.out.println("配置文件行:" + configLines);
        System.out.println("总行:" + allLines);
    }

    /**
     * 循环文件夹统计
     * @param filePath
     */
    private void listFile(String filePath) {
        File f = new File(filePath);
        File[] childs = f.listFiles();

        for (int i = 0; i < childs.length; i++) {
            if (!childs[i].isDirectory()) {
                if (childs[i].getName().matches(".*\\.java$")
                        ||childs[i].getName().endsWith(".yml")
                        || childs[i].getName().endsWith(".xml")
//                    || childs[i].getName().endsWith(".properties")
                ) {
                    System.out.println(childs[i].getName());
                    sumCode(childs[i]);
                }
            }else {
                listFile(childs[i].getPath());
            }
        }
    }

    /**
     * 统计代码行数
     * @param file
     */
    private void sumCode(File file){
        BufferedReader br = null;
        try{
            br = new BufferedReader(new FileReader(file));
            String line = "";
            while ((line = br.readLine()) != null){
                allLines++;
                if(file.getName().endsWith(".yml")
                        || file.getName().endsWith(".xml")
                        || file.getName().endsWith(".properties")){
                    //配置文件
                    configLines++;
                }else {
                    //java文件
                    String trimStr = line.trim();
                    if (trimStr.length() == 0){
                        //空行
                        nullLines++;
                    }else if (trimStr.startsWith("//")
                            || trimStr.startsWith("/**")
                            || trimStr.startsWith("*")
                            || trimStr.startsWith("*/")
                            || trimStr.startsWith("/*")
                    ){
                        annoLines++;
                    }else {
                        codeLines++;
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值