package
com.yhj.common.counter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import com.yhj.common.io.FileUtil;
/**
* com.yhj.common.counter.CodeCounter.java
* be created by 2009-12-10 下午05:21:31+
* 本类的主要功能:
* <p>
* 统计某个文件夹下文件 Java文件 以及
* 统计指定目录下以及其子目录下的所有java文件中代码行数 注释数 空行数等
* </p>
* @author 一线天色 天宇星辰
*
*/
public class CodeCounter {
static long codeLines = 0 ; // 代码行数
static long commentLines = 0 ; // 注释行署
static long blankLines = 0 ; // 空白行数
static long totalFileCount = 0 ; // 总共文件数目
static long totalFolderCount = 0 ; // 文件夹文件数目
static long javaFileCount = 0 ; // Java文件数目
static boolean isOne = false ; // 将每次生成的文件生成到一个文件中的标示
static ArrayList < File > fileArray = new ArrayList < File > ();
private static String fileName = null ;
/**
* 可以统计指定目录下以及其子目录下的所有java文件中代码
*/
public static void codeCounter(String path) {
// 统计程序运行时间
long beginTime = System.currentTimeMillis();
// 统计运行消耗的内存
long beginMemory = Runtime.getRuntime().freeMemory();
// 要统计的文件夹
File file = new File(path);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
String beginDetailTime = simpleDateFormat.format( new Date());
System.out.println( " ===================================== " );
writeToLog( " ===================================== " );
System.out.println( " 开始扫描文件夹 " + path + " ... " );
writeToLog( " 开始扫描文件夹 " + path + " ... " );
// 存储文件的数据
ArrayList < File > al = getFile(file);
System.out.println( " ===================================== " );
writeToLog( " ===================================== " );
System.out.println( " 开始统计文件... " );
writeToLog( " 开始统计文件... " );
for (File f : al) {
if (f.getName().matches( " .*\\.java$ " )) // 匹配java格式的文件
{
System.out.println( " 正在统计文件: " + f.getPath());
writeToLog( " 正在统计文件: " + f.getPath());
javaFileCount ++ ;
count(f);
}
}
System.out.println( " 完成统计! " );
writeToLog( " 完成统计! " );
System.out.println( " ===================================== " );
writeToLog( " ===================================== " );
System.out.println( " 统计结果如下: " );
writeToLog( " 统计结果如下: " );
System.out.println( " 文件夹总数: " + totalFolderCount + " 个 " );
writeToLog( " 文件夹总数: " + totalFolderCount + " 个 " );
System.out.println( " 文件总个数: " + totalFileCount + " 个 " );
writeToLog( " 文件总个数: " + totalFileCount + " 个 " );
System.out.println( " Java文件数: " + javaFileCount + " 个 " );
writeToLog( " Java文件数: " + javaFileCount + " 个 " );
System.out.println( " 代码行数: " + codeLines);
writeToLog( " 代码行数: " + codeLines);
System.out.println( " 注释行数: " + commentLines);
writeToLog( " 注释行数: " + commentLines);
System.out.println( " 空白行数: " + blankLines);
writeToLog( " 空白行数: " + blankLines);
long endMemory = Runtime.getRuntime().freeMemory();
long billMemory = beginMemory - endMemory;
long endTime = System.currentTimeMillis();
long sec = (endTime - beginTime) / 1000 ;
System.out.println( " 程序开始时间: " + beginDetailTime);
writeToLog( " 程序开始时间: " + beginDetailTime);
String endDetailTime = simpleDateFormat.format( new Date());
System.out.println( " 程序结束时间: " + endDetailTime);
writeToLog( " 程序结束时间: " + endDetailTime);
System.out.println( " 程序运行共耗时间: " + sec / 60 + " 分 " + sec % 60 + " 秒 " );
writeToLog( " 程序运行共耗时间: " + sec / 60 + " 分 " + sec % 60 + " 秒 " );
System.out.println( " 程序运行共耗内存: " + billMemory * 1.0 / 1024 + " KB " );
writeToLog( " 程序运行共耗内存: " + billMemory * 1.0 / 1024 + " KB " );
}
// 获得目录下的文件和子目录下的文件
private static ArrayList < File > getFile(File f) {
System.out.println( " 开始扫描子文件夹 " + f.getPath() + " ... " );
writeToLog( " 开始扫描子文件夹 " + f.getPath() + " ... " );
File[] ff = f.listFiles();
for (File child : ff) {
if (child.isDirectory()) {
System.out.println( " 发现文件夹: " + child.getPath());
writeToLog( " 发现文件夹: " + child.getPath());
totalFolderCount ++ ;
getFile(child);
} else
{
fileArray.add(child);
totalFileCount ++ ;
System.out.println( " 发现文件: " + child.getPath());
writeToLog( " 发现文件: " + child.getPath());
}
}
System.out.println( " 文件夹 " + f.getPath() + " 扫描结束! " );
writeToLog( " 文件夹 " + f.getPath() + " 扫描结束! " );
return fileArray;
}
// 统计文件
private static void count(File f) {
BufferedReader br = null ;
boolean flag = false ;
try {
br = new BufferedReader( new FileReader(f));
String line = "" ;
while ((line = br.readLine()) != null ) {
line = line.trim(); // 除去注释前的空格
if (line.matches( " ^[ ]*$ " )) { // 匹配空行
blankLines ++ ;
} else if (line.startsWith( " // " )) {
commentLines ++ ;
} else if (line.startsWith( " /* " ) && ! line.endsWith( " */ " )) {
commentLines ++ ;
flag = true ;
} else if (line.startsWith( " /* " ) && line.endsWith( " */ " )) {
commentLines ++ ;
} else if (flag == true ) {
commentLines ++ ;
if (line.endsWith( " */ " )) {
flag = false ;
}
} else {
codeLines ++ ;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null ) {
try {
br.close();
br = null ;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static void writeToLog(String data) {
try {
if ( ! isOne)
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat( " yyyy-MM-dd HH.mm.ss " );
fileName = simpleDateFormat.format( new Date()) + " .log " ;
isOne = true ;
}
FileUtil.writeStringToFile( " F:/YHJ日志生成区 " ,fileName, data + " \n " , " UTF-8 " , true );
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
codeCounter( " C:/Program Files " );
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import com.yhj.common.io.FileUtil;
/**
* com.yhj.common.counter.CodeCounter.java
* be created by 2009-12-10 下午05:21:31+
* 本类的主要功能:
* <p>
* 统计某个文件夹下文件 Java文件 以及
* 统计指定目录下以及其子目录下的所有java文件中代码行数 注释数 空行数等
* </p>
* @author 一线天色 天宇星辰
*
*/
public class CodeCounter {
static long codeLines = 0 ; // 代码行数
static long commentLines = 0 ; // 注释行署
static long blankLines = 0 ; // 空白行数
static long totalFileCount = 0 ; // 总共文件数目
static long totalFolderCount = 0 ; // 文件夹文件数目
static long javaFileCount = 0 ; // Java文件数目
static boolean isOne = false ; // 将每次生成的文件生成到一个文件中的标示
static ArrayList < File > fileArray = new ArrayList < File > ();
private static String fileName = null ;
/**
* 可以统计指定目录下以及其子目录下的所有java文件中代码
*/
public static void codeCounter(String path) {
// 统计程序运行时间
long beginTime = System.currentTimeMillis();
// 统计运行消耗的内存
long beginMemory = Runtime.getRuntime().freeMemory();
// 要统计的文件夹
File file = new File(path);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
String beginDetailTime = simpleDateFormat.format( new Date());
System.out.println( " ===================================== " );
writeToLog( " ===================================== " );
System.out.println( " 开始扫描文件夹 " + path + " ... " );
writeToLog( " 开始扫描文件夹 " + path + " ... " );
// 存储文件的数据
ArrayList < File > al = getFile(file);
System.out.println( " ===================================== " );
writeToLog( " ===================================== " );
System.out.println( " 开始统计文件... " );
writeToLog( " 开始统计文件... " );
for (File f : al) {
if (f.getName().matches( " .*\\.java$ " )) // 匹配java格式的文件
{
System.out.println( " 正在统计文件: " + f.getPath());
writeToLog( " 正在统计文件: " + f.getPath());
javaFileCount ++ ;
count(f);
}
}
System.out.println( " 完成统计! " );
writeToLog( " 完成统计! " );
System.out.println( " ===================================== " );
writeToLog( " ===================================== " );
System.out.println( " 统计结果如下: " );
writeToLog( " 统计结果如下: " );
System.out.println( " 文件夹总数: " + totalFolderCount + " 个 " );
writeToLog( " 文件夹总数: " + totalFolderCount + " 个 " );
System.out.println( " 文件总个数: " + totalFileCount + " 个 " );
writeToLog( " 文件总个数: " + totalFileCount + " 个 " );
System.out.println( " Java文件数: " + javaFileCount + " 个 " );
writeToLog( " Java文件数: " + javaFileCount + " 个 " );
System.out.println( " 代码行数: " + codeLines);
writeToLog( " 代码行数: " + codeLines);
System.out.println( " 注释行数: " + commentLines);
writeToLog( " 注释行数: " + commentLines);
System.out.println( " 空白行数: " + blankLines);
writeToLog( " 空白行数: " + blankLines);
long endMemory = Runtime.getRuntime().freeMemory();
long billMemory = beginMemory - endMemory;
long endTime = System.currentTimeMillis();
long sec = (endTime - beginTime) / 1000 ;
System.out.println( " 程序开始时间: " + beginDetailTime);
writeToLog( " 程序开始时间: " + beginDetailTime);
String endDetailTime = simpleDateFormat.format( new Date());
System.out.println( " 程序结束时间: " + endDetailTime);
writeToLog( " 程序结束时间: " + endDetailTime);
System.out.println( " 程序运行共耗时间: " + sec / 60 + " 分 " + sec % 60 + " 秒 " );
writeToLog( " 程序运行共耗时间: " + sec / 60 + " 分 " + sec % 60 + " 秒 " );
System.out.println( " 程序运行共耗内存: " + billMemory * 1.0 / 1024 + " KB " );
writeToLog( " 程序运行共耗内存: " + billMemory * 1.0 / 1024 + " KB " );
}
// 获得目录下的文件和子目录下的文件
private static ArrayList < File > getFile(File f) {
System.out.println( " 开始扫描子文件夹 " + f.getPath() + " ... " );
writeToLog( " 开始扫描子文件夹 " + f.getPath() + " ... " );
File[] ff = f.listFiles();
for (File child : ff) {
if (child.isDirectory()) {
System.out.println( " 发现文件夹: " + child.getPath());
writeToLog( " 发现文件夹: " + child.getPath());
totalFolderCount ++ ;
getFile(child);
} else
{
fileArray.add(child);
totalFileCount ++ ;
System.out.println( " 发现文件: " + child.getPath());
writeToLog( " 发现文件: " + child.getPath());
}
}
System.out.println( " 文件夹 " + f.getPath() + " 扫描结束! " );
writeToLog( " 文件夹 " + f.getPath() + " 扫描结束! " );
return fileArray;
}
// 统计文件
private static void count(File f) {
BufferedReader br = null ;
boolean flag = false ;
try {
br = new BufferedReader( new FileReader(f));
String line = "" ;
while ((line = br.readLine()) != null ) {
line = line.trim(); // 除去注释前的空格
if (line.matches( " ^[ ]*$ " )) { // 匹配空行
blankLines ++ ;
} else if (line.startsWith( " // " )) {
commentLines ++ ;
} else if (line.startsWith( " /* " ) && ! line.endsWith( " */ " )) {
commentLines ++ ;
flag = true ;
} else if (line.startsWith( " /* " ) && line.endsWith( " */ " )) {
commentLines ++ ;
} else if (flag == true ) {
commentLines ++ ;
if (line.endsWith( " */ " )) {
flag = false ;
}
} else {
codeLines ++ ;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null ) {
try {
br.close();
br = null ;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static void writeToLog(String data) {
try {
if ( ! isOne)
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat( " yyyy-MM-dd HH.mm.ss " );
fileName = simpleDateFormat.format( new Date()) + " .log " ;
isOne = true ;
}
FileUtil.writeStringToFile( " F:/YHJ日志生成区 " ,fileName, data + " \n " , " UTF-8 " , true );
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
codeCounter( " C:/Program Files " );
}
}