统计java文件中的代码行数

博客介绍了统计Java代码行数的工具类CodeCounterUtil.java,包含统计指定目录下Java文件代码行数的getCodeNumFromFolder方法,以及统计具体文件中Java代码行数的getCodeNumFromFile方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

统计Java代码行数工具类  ——  CodeCounterUtil.java

  1. 统计指定目录下的java文件中代码行数  ——  public static int  getCodeNumFromFolder(String filePath)
  2. 统计具体文件中的java代码行数  ——  public static int getCodeNumFromFile(String filePath)
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * 统计java文件中的代码行数
 */
public class CodeCounterUtil {

    /**
     * 统计的文件数量
     */
    private static long files = 0;
    /**
     * 代码行数
     */
    private static int codeLines = 0;
    /**
     * 注释行数
     */
    private static int commentLines = 0;
    /**
     * 空行数量
     */
    private static int blankLines = 0;
    /**
     * 文件数组
     */
    private static ArrayList<File> fileArray = new ArrayList<File>();

    /**
     * 函数功能:统计指定目录下(文件夹中)java文件中的代码行数
     *
     * @param filePath 文件夹路径
     * @return 代码总行数
     */
    public static int getCodeNumFromFolder(String filePath) {
        String path = filePath.replace("target/test-classes", "src");

        ArrayList<File> al = getFile(new File(path));
        for (File f : al) {
            // 匹配java格式的文件
            if (f.getName().matches(".*\\.java$")) {
                count(f);
            }
        }
        System.out.println("代码行数:" + codeLines);
        System.out.println("注释行数:" + commentLines);
        System.out.println("空白行数:" + blankLines);
        return codeLines + commentLines + blankLines;
    }

    /**
     * 函数功能:获得目录下的文件和子目录下的文件
     *
     * @param f 目录
     * @return ArrayList<File>
     */
    private static ArrayList<File> getFile(File f) {
        File[] ff = f.listFiles();
        if (ff != null) {
            for (File child : ff) {
                if (child.isDirectory()) {
                    getFile(child);
                } else {
                    fileArray.add(child);
                }
            }
        }
        return fileArray;
    }

    /**
     * 函数功能:统计具体java文件中的代码行数
     *
     * @param f 具体的java文件
     */
    private static void count(File f) {
        BufferedReader br = null;
        boolean flag = false;
        try {
            br = new BufferedReader(new FileReader(f));
            String line = "";
            while ((line = br.readLine()) != null) {
                // 除去注释前的空格
                line = line.trim();
                // 匹配空行
                if (line.matches("^[ ]*$")) {
                    blankLines++;
                } else if (line.startsWith("//")) {
                    commentLines++;
                } else if (line.startsWith("/*") && !line.endsWith("*/")) {
                    commentLines++;
                    flag = true;
                } else if (line.startsWith("/*") && line.endsWith("*/")) {
                    commentLines++;
                } else if (flag) {
                    commentLines++;
                    if (line.endsWith("*/")) {
                        flag = false;
                    }
                } else {
                    codeLines++;
                }
            }
            files++;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 函数功能:获取具体的Java文件中的代码行数
     *
     * @param filePath 文件路径
     * @return 具体文件中的代码行数
     */
    public static int getCodeNumFromFile(String filePath) {
        File fileName = new File(filePath);
        if (fileName.getName().matches(".*\\.java$")) {
            count(fileName);
        }
        int codeNum = codeLines + blankLines + commentLines;
        System.out.println("代码总行数:" + codeNum);
        return codeNum;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值