项目代码行数统计器,帮助你统计项目一共有多少行代码
2016-07-08 00:00
369 查看
记得以前有位大师说,一个程序员如果没有写满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生成缩略图之Thumbnailator
- 使用java读取大文本文件(2G)
- Eclipse控制台乱码
- Java深度历险(六)——Java注解
- Think in Java 读书笔记(1)
- FastDFS的配置、部署与API使用解读(3)以流的方式上传文件的客户端代码
- Java深度历险(三)——Java线程:基本概念、可见性与同步
- Java REST框架一览
- CAS研究(四)-登出/logout
- Eclipse中建立JSR181的web服务
- Java深度历险(七)——Java反射与动态代理
- FastDFS的配置、部署与API使用解读(2)以字节方式上传文件的客户端代码
- Java深度历险(四)——Java垃圾回收机制与引用类型
- Spring Bean 初始化过程
- 使用Spring @Async异步执行方法的笔记
- java中abstract详解
- java中的匿名内部类总结
- 在代码中修改TextView的DrawableRight图片
- 使用Eclipse-Maven-git做Java开发(17)--maven项目初步
- spring 简介(译)