如何统计项目的类 项目完成了,现在经理想做个统计,就是说统计项目中一共有多少的配置文件,类,JSP页面。还有多少的代码,请问这个有什么方法实现么? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 贴个代码,是统计项目java代码量的,其他的可以相同的思路吧package util;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;public class CodeCounter { //有效行数 private static long normalLines = 0; //注释行数 private static long commentLines = 0; //空白行 private static long whiteLines = 0; private ArrayList<File> al = new ArrayList<File>(); /** * 递归找到该目录下的所有java源文件 * @param homeDir */ public void findJavaSource(File homeDir) { if(homeDir.isFile()){ al.add(homeDir); return; } File[] f = homeDir.listFiles(); for (File child : f) { if (child.isFile() && child.getName().matches(".*\\.java$")) { al.add(child); } else if (child.isDirectory()) { findJavaSource(child); } } } public static void main(String []str){ CodeCounter cc = new CodeCounter(); //测试时别忘了该目录地址 File homeDir = new File("E:\\workspace_eclipse_3.3.2\\GCSMS1.0\\src"); cc.count(homeDir); } public void count(File homeDir) { this.findJavaSource(homeDir); for (File child : al) { parse(child); } System.out.println("normalLines:" + normalLines); System.out.println("commentLines:" + commentLines); System.out.println("whiteLines:" + whiteLines); } private static void parse(File f) { BufferedReader br = null; boolean comment = false; try { br = new BufferedReader(new FileReader(f)); String line = ""; while ((line = br.readLine()) != null) { line = line.trim(); if (line.matches("^[\\s&&[^\\n]]*$")) { whiteLines++; } else if (line.startsWith("/*") && !line.endsWith("*/")) { commentLines++; comment = true; } else if (line.startsWith("/*") && line.endsWith("*/")) { commentLines++; } else if (true == comment) { commentLines++; if (line.endsWith("*/")) { comment = false; } } else if (line.startsWith("//")) { commentLines++; } else { normalLines++; } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); br = null; } catch (IOException e) { e.printStackTrace(); } } } }} 1楼的,你的 for (File child : f) {只能是在JDK1.5下运行,我的是JDK1.4郁闷 有工具,可以统计代码的行数,包括jsp和java文件,以前用过,忘了叫什么名字啦,回头我给你找一下吧 I am trying to add an UI, maybe in two days... 发现个0.0/0.0的问题,请教高手是为什么??? 正则高手!!!!!!!!!!!!!最后一个问题!实在写不出来了..虽然现在正则水平有提高但是类似这样的还是写不出啊 ,MyEclipse_6.0GA_E3.3_FullStackInstaller.exe与eclipse-jee-europa-win32.zip区别 求高人指点 学习java的好地方!!! 这里有一个程序,帮忙看看 java 中连oracle 报错 请大家帮忙看看 请教:正则表达式如何解析JSON 请教:jsp中datetime问题 JAVA一个例子没看懂 jdk的問題 呵呵 一个javascript的小问题??
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;public class CodeCounter { //有效行数
private static long normalLines = 0;
//注释行数
private static long commentLines = 0;
//空白行
private static long whiteLines = 0; private ArrayList<File> al = new ArrayList<File>(); /**
* 递归找到该目录下的所有java源文件
* @param homeDir
*/
public void findJavaSource(File homeDir) {
if(homeDir.isFile()){
al.add(homeDir);
return;
}
File[] f = homeDir.listFiles();
for (File child : f) {
if (child.isFile() && child.getName().matches(".*\\.java$")) {
al.add(child);
} else if (child.isDirectory()) {
findJavaSource(child);
}
}
} public static void main(String []str){
CodeCounter cc = new CodeCounter();
//测试时别忘了该目录地址
File homeDir = new File("E:\\workspace_eclipse_3.3.2\\GCSMS1.0\\src");
cc.count(homeDir);
}
public void count(File homeDir) {
this.findJavaSource(homeDir);
for (File child : al) {
parse(child);
}
System.out.println("normalLines:" + normalLines);
System.out.println("commentLines:" + commentLines);
System.out.println("whiteLines:" + whiteLines);
} private static void parse(File f) {
BufferedReader br = null;
boolean comment = false;
try {
br = new BufferedReader(new FileReader(f));
String line = "";
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.matches("^[\\s&&[^\\n]]*$")) {
whiteLines++;
} else if (line.startsWith("/*") && !line.endsWith("*/")) {
commentLines++;
comment = true;
} else if (line.startsWith("/*") && line.endsWith("*/")) {
commentLines++;
} else if (true == comment) {
commentLines++;
if (line.endsWith("*/")) {
comment = false;
}
} else if (line.startsWith("//")) {
commentLines++;
} else {
normalLines++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}}
我的是JDK1.4郁闷
有工具,可以统计代码的行数,包括jsp和java文件,以前用过,忘了叫什么名字啦,回头我给你找一下吧