[JAVA实用应用] 项目代码行数统计器,帮助你统计项目一共有多少行代码

记得以前有位大师说,一个程序员如果没有写满1,000,000行代码,那么不是一个成熟的程序员.
[JAVA实用应用] 项目代码行数同机器,帮助你统计项目一共有多少行代码
/**

 * codelinesstatistic 2008-6-20 TangRen

 * Copyright www.tonydev.cn 2008

 */

package cn.javadr.goodlet.codelinesstatistic;



import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;



/**

 * @author Tang Ren

 * @email: <a href="mailto:tangren1206@163.com">tangren1206@163.com</a>

 * 

 * 代码行数统计器

 */

public class CodeLinesStatistic {



	private static long sums = 0;



	private static String suffixs[];



	private static String target;



	/** buffer flush 的最大值 * */

	private static int FLUSH_FLAG = 1024 * 64;



	private static StringBuffer statistics = new StringBuffer();



	/**

	 * @param args

	 */

	public static void main(String... args) throws IOException {

		// 这里模拟命令行下的参数进行测试

		args = new String[] { "F:/Dev/alisoft_space/ExtSysDataImportTask", // 这里是项目的根目录

				"java", "xml", "properties" }; // 这里是统计文件的后缀名



		long startTimes = System.currentTimeMillis();

		if (args.length > 1)

			suffixs = new String[args.length - 1];

		else {

			System.out

					.println("As that : targetLocation , fileSuffix , fileSuffix . . .");

			return;

		}

		for (int i = 0; i < args.length; i++) {

			if (i == 0) {

				target = args[i];

			} else {

				suffixs[i - 1] = args[i];

			}

		}

		File targetFile = new File(target);

		if (targetFile.exists()) {

			statistic(targetFile);

			System.out.print(statistics.toString());

			System.out.println("All completement. U write [" + sums

					+ "] lines code. did great job!");

		} else {

			System.out.println("File or Dir not exist : " + target);

		}

		System.out.println("Total times "

				+ (System.currentTimeMillis() - startTimes) + " ms");

	}



	/**

	 * 深度优先,统计文件行数

	 * 

	 * @param file

	 * @throws IOException

	 */

	private static void statistic(File file) throws IOException {

		if (file.isDirectory()) {

			File[] files = file.listFiles();

			for (int i = 0; i < files.length; i++) {

				statistic(files[i]);

			}

		}

		if (file.isFile()) {

			if (isMatchSuffixs(file)) {

				sums += countFileTextLines(file);

			}

		}

	}



	/**

	 * 检查文件是否是制定后缀的文件

	 * 

	 * @param file

	 * @return

	 */

	private static boolean isMatchSuffixs(File file) {

		String fileName = file.getName();

		if (fileName.indexOf(".") != -1) {

			String extName = fileName.substring(fileName.indexOf(".") + 1);

			for (int i = 0; i < suffixs.length; i++) {

				if (suffixs[i].equals(extName)) {

					return true;

				}

			}

		}

		return false;

	}



	/**

	 * 统计文件行数

	 * 

	 * @param file

	 * @return

	 * @throws IOException

	 */

	private static long countFileTextLines(File file) throws IOException {

		long result = 0;

		if (statistics.length() > FLUSH_FLAG) {

			System.out.print(statistics.toString());

			statistics = new StringBuffer();

		}

		statistics.append("Counting in ").append(file.getAbsolutePath());

		BufferedReader br = new BufferedReader(new FileReader(file));

		while (br.readLine() != null)

			result++;

		br.close();

		statistics.append("   -  ").append(result).append("/n");

		return result;

	}

}

刚刚断网, 无聊间写了一个统计一个项目中代码行数的小应用.

因为记得以前有位大师说, 没写过超过1,000,000行代码的程序员不是经验丰富的程序员.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值