2018-01-26项目代码行数统计

本文介绍了一个用于统计Java项目中特定文件类型的代码行数的工具。该工具能够递归地搜索指定目录下所有符合预设命名规则的Java源文件和Mapper配置文件,并统计非空行数。

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

package com.project.controller;

import org.springframework.util.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: CountLine
 * @Description: 我这个类专门针对我这边的项目,如果需要统计自行更改
 * @Author: Looveh
 * @CreateDate: 2018/1/26 15:08
 * @Version: v1.0
 */
public class CountLine {

    private static Integer COUNT_LINE = 0;

    public static void main(String[] args) {

        String rootPath = "E:\\caishang\\";
        //获取文件集合
        List<File> fileList = getFiles(rootPath);
//        System.out.println(fileList.size());
        int countLine = 0;
        for (int i = 0; i < fileList.size(); i++) {
            System.out.println(fileList.get(i).getName());
            //统计集合里面所有文件非空的行数
            countLine += countLine(fileList.get(i));
        }

        System.out.println(countLine);
    }

    /**
     * @方法说明
     * @method getFiles
     * @author hp
     * @date 2018/1/26 16:43
     * @param path
     * @return java.util.List<java.io.File>
     * @desc [获取path下面所有CS开头,.Java结尾或者Mapper.xml结尾的文件]
     */
    public static List<File> getFiles(String path){

        File root = new File(path);
        List<File> fileList = new ArrayList<>();
        //不是文件夹
        if(!root.isDirectory()){
            String fileName = root.getName();
            int sufIndex = fileName.lastIndexOf(".");
            //CS开头并且后缀为.Java或者以Mapper.xml结尾
            //注意:如果项目中没有其他项目的文件,可以不加判断,直接统计所有文件,自行判断
            if(sufIndex != -1 && fileName.startsWith("CS") && (".java".equals(fileName.substring(sufIndex)) || fileName.endsWith("Mapper.xml"))){
                fileList.add(root);
            }
        }else{
            //文件夹使用递归
            File[] subFile = root.listFiles();
            for (File file : subFile){
                if(!file.getName().contains("idea") && !file.getName().contains("job") && !file.getName().contains("svn")){
                    fileList.addAll(getFiles(file.getAbsolutePath()));
                }
            }
        }

        return fileList;
    }

    /**
     * @方法说明
     * @method countLine
     * @author hp
     * @date 2018/1/26 16:07
     * @return int
     * @desc [统计行数]
     */
    public static int countLine(File file){
        //空行数
        int spaceLine = 0;
        //文件行数
        int line = 0;
        InputStreamReader reader = null;
        try {
            //创建一个输入流对象
            reader = new InputStreamReader(new FileInputStream(file));
            //创建一个对象转将文件转换成计算机识别的文件
            BufferedReader br = new BufferedReader(reader);
            String str = br.readLine();
            line+=1;
            while (!StringUtils.isEmpty(str) || spaceLine < 10){
                str = br.readLine();
                //如果这一行为空行,空行+1并跳过这一行
                if(StringUtils.isEmpty(str)){
                    spaceLine+=1;
                    continue;
                }
                //如果空行数达到10行默认算该文件已经读完【可以自行设置】
                if(spaceLine == 10){
                    break;
                }else{
                    line+=1;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return line;
    }
}

欢迎大神补充。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值