`
yunchow
  • 浏览: 318276 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

简单统计代码行数

阅读更多
真的很多,我刚写了个程序统计了一下,我们项目才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();
		}
	}
	
	

}













分享到:
评论
1 楼 Copperfield 2011-03-25  
顶你的头像

相关推荐

Global site tag (gtag.js) - Google Analytics