项目代码行数统计器,帮助你统计项目一共有多少行代码
2016-07-29 00:00
459 查看
记得以前有位大师说,一个程序员如果没有写满1,000,000行代码,那么不是一个成熟的程序员.
/**
* 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行代码的程序员不是经验丰富的程序员.
/**
* 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行代码的程序员不是经验丰富的程序员.
相关文章推荐
- [JAVA实用应用] 项目代码行数统计器,帮助你统计项目一共有多少行代码
- 项目代码行数统计器,帮助你统计项目一共有多少行代码
- 项目代码行数统计器,帮助你统计项目一共有多少行代码
- 项目管理之源码目录下的代码行数统计
- 统计项目的代码行数(python处女作)
- .net winform下 C#/VB.NET项目代码行数统计(2009-6-25新版本)
- Xcode 统计项目代码行数及常用快捷键
- 统计Xcode项目代码行数
- eclipse统计整个项目的代码行数
- Java代码 统计项目的文件数量,和代码行数
- 递归统计项目中的非空白代码行数
- 统计项目中的代码行数
- 项目代码行数统计--java类统计
- 一条shell指令统计项目的代码行数
- 统计项目代码行数Count Lines of Code(CLOC)
- 统计Xcode项目代码行数
- 递归统计项目中的非空白代码行数
- Eclipse中统计项目代码行数技巧
- 统计项目代码行数程序
- 在Eclipse中统计项目的代码行数