您的位置:首页 > 编程语言 > Java开发

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

2008-06-21 00:15 746 查看
记得以前有位大师说,一个程序员如果没有写满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行代码的程序员不是经验丰富的程序员.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: