统计代码行数(超实用,提高效率的工具)

本文介绍了一种提高效率的方法,通过修改指定目录和文件格式,使用特定工具在Spring项目启动后,可以在控制台自动统计代码行数。

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

代码如下:

package com.utils.countCodeNum;

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 codeLines = 0; // 代码行
	static long allLines = 0; // 代码行

	public static void main(String[] args) throws Exception {
		File f = new File("E:\\jxtech\\igy-front\\igy-dao"); // 目录
		String type = ".xml";// 查找什么类型的代码,如".java"就是查找以java开发的代码量,".php"就是查找以PHP开发的代码量
		CalculateRows.treeFile(f, type);
		System.out.println("路径:" + f.getPath());
		System.out.println(type + "类Class数量:" + classcount);
		System.out.println("代码数量:" + codeLines);
		System.out.println("注释数量:" + commentLines);
		System.out.println("空行数量:" + normalLines);
		if (classcount == 0) {
			System.out.println("代码平均数量:" + 0);
		} else {
			System.out.println("代码平均数量:" + codeLines / classcount);
		}
		System.out.println("总代码行:" + allLines);
	}

	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 {// 其他的就是代码行
							codeLines++;
						}
					}
					if (br != null) {
						br.close();
						br = null;
					}
				}
			} else {
				treeFile(childs[i], type);
			}
		}
	}
}

注:1.修改需要统计的目录(File所指向的目录)

        2.修改需要统计的文件格式

        3.spring项目启动之后即可再控制台输出对应的代码等行数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值