论坛首页 入门技术论坛

今天编的java代码统计

浏览 2625 次
该帖已经被评为新手帖
作者 正文
   发表时间:2009-10-04   最后修改:2009-10-06
今天想试试学了很久的正则表达式,就编了一个小小的程序(朋友们要是有意见就提吧):
package regular.expression.codecounter;
import java.io.*;
public class CodeCounter {
	private static int normallines=0;
	private static int commentlines=0;
	private static int whitelines=0;
	public static void main(String[] args) {
		File file=new File("d:\\code");
		File[] files=file.listFiles();
		for(File file:files){
			if(file.getName().matches(".*\\.java$"))
				start(file);
		}
		System.out.println("normalliens="+normallines);
		System.out.println("commentliens="+commentlines);
		System.out.println("whiteliens="+whitelines);
	}
	private static void start(File file) {
		BufferedReader br=null;
		boolean flag=false;
		try {
			br=new BufferedReader(new FileReader(child));
			String line="";
			while((line=br.readLine())!=null){
				line=line.trim();
				if(line.matches("^[\\s&&[^\\n]]*")){
					whitelines++;
				}else if(line.startsWith("/*")&&!line.endsWith("*/")){
					commentlines++;
					flag=true;
				}else if(true==flag){
					commentlines++;
					if(line.endsWith("*/")){
						flag=false;
					}
				}else if (line.startsWith("/*")&&line.endsWith("*/")) {
					commentlines++;
				}else if(line.matches("^//.*")){
					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();
				}
			}
		}	
	}
}
   发表时间:2009-10-06  
程序虽然不长,总该说说你的程序是干嘛的吧?谁有空把每个帖子的程序都看一遍,然后在决定是否跟帖?
0 请登录后投票
   发表时间:2009-10-06  
看那3个静态变量的名称
LZ看来是想统计java源代码中各种代码的行数

话说那3个变量干嘛用大写, 我第一眼还以为是常量
0 请登录后投票
   发表时间:2009-10-06  
liu78778 写道


话说那3个变量干嘛用大写, 我第一眼还以为是常量


同感!!!
0 请登录后投票
   发表时间:2009-10-06  
哇塞。。。高了这么多的else if。。。程序有点难看
0 请登录后投票
   发表时间:2009-10-27  
大眼一看 程序只支持一层目录
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics