简单统计代码行数

真的很多,我刚写了个程序统计了一下,我们项目才695个类

并符上测试程序,请各位指点 -->


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;


/**
* File: Counter.java
* User: z3y2
* Date: 2010-12-30
* Time: 下午04:58:03
* Copyright: (c) 2010 All Rights Reserved
*/

/**
* @author z3y2
*/
public class Counter {

static long l = 0;

static long fileCount = 0;

static long nullLineCount = 0;

static long total = 0;

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {

String path = Counter.class.getResource("/").getPath().substring(1);
java.io.File classpath = new java.io.File(path);
String srcpath = classpath.getParentFile().getParentFile().getParentFile().getAbsolutePath();

if (!srcpath.endsWith(File.separator)) {
srcpath += File.separator;
}
srcpath += "src" + File.separator;

File srcFile = new File(srcpath);

readFile(srcFile);

System.out.println("共处理文件数:" + fileCount);
System.out.println("源代码共有行数:" + total + ", 其中代码行数为:" + l + ", 空白行为:" + nullLineCount);

}


static void readFile(File file) throws Exception {
if (file.isDirectory()) {
for (File f : file.listFiles()) {
readFile(f);
}
} else if (file.getName().endsWith(".java")) {

fileCount ++;

System.out.println("正在处理文件:" + file.getAbsolutePath());

FileInputStream in = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(in));

for (String line = br.readLine(); line != null; line = br.readLine()) {
if (line.trim().length() == 0) {
nullLineCount ++;
} else {
l ++;
}

total ++;
}

in.close();
br.close();
}
}



}


你可能感兴趣的:(code,thinking)