今天编的java代码统计

今天想试试学了很久的正则表达式,就编了一个小小的程序(朋友们要是有意见就提吧):
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();
				}
			}
		}	
	}
}

你可能感兴趣的:(java,正则表达式)