爪哇狼

博客园 首页 新随笔 联系 订阅 管理
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


public class CountCode {
    static int nomalCount = 0;//代码行
    static int commonCount = 0;//注释行
    static int whiteCount = 0;//空白行
    public static void main(String[] args) {
        File f = new File("D:\\testjava");
        File[] files = f.listFiles();
        for(int i=0;i<files.length;i++){
            File ff = files[i];
            System.out.println(ff.getName());
            if(ff.getName().matches(".*\\.java$")){
                System.out.println("000000");
                
                parse(ff);
            }
        }
        System.out.println("nomoalCount="+nomalCount);
        System.out.println("commonCount="+commonCount);
        System.out.println("whiteCount="+whiteCount);
    }

    private static void parse(File f) {
        BufferedReader br = null;
        boolean flag = false;
        try {
            br = new BufferedReader(new FileReader(f));
            String line = "";
            while((line = br.readLine()) != null){
          line = line.trim();
if(line.matches("^[\\s&&[^\\n]]*$")){//空白行正则表达式 whiteCount++; }else if(line.startsWith("/*")&& !line.endsWith("*/")){ commonCount++; flag = true; }else if(true == flag){ commonCount++; if(line.endsWith("*/")){ System.out.println("sdfsd"); flag = false; } }else{ nomalCount++; } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

 

posted on 2015-01-23 00:01  爪哇狼  阅读(657)  评论(0编辑  收藏  举报