程序员代码量统计

2021-03-03  本文已影响0人  哎一古啊
代码量的多少不能证明程序员的能力!

该程序以Java语言编写
以统计Java语言写的项目代码量为例:

public class Files {
 
    static long whiteLine = 0;
    static long comentLine = 0;
    static long sormaLine = 0;
     
    public static void main(String[] args) {
        File f = new File("填写路径,不用具体到文件名,只要路径下有.java文件就可以");
        getChildren(f);
        System.out.println("空行:"+whiteLine);
        System.out.println("注释行:"+comentLine);
        System.out.println("有效行:"+sormaLine);
        System.out.println("总行数:"+(whiteLine+comentLine+sormaLine));

    }
    private static void getChildren(File file){

          if(file.isFile()){
            //如果为单个文件直接开始读取行数
            preas(file);
          }else if(file.isDirectory()) {
            //如果为文件目录,得到目录下的子文件以及子文件夹
             File[] codeFiles = file.listFiles();
             for(File child:codeFiles){
                getChildren(child);  //反复读取
             }
          }

     }

    private static void preas(File f){
        BufferedReader br = null;
        Boolean comPd = false;
        try {
            br = new BufferedReader(new FileReader(f));
            String readLine = null;
            while((readLine = br.readLine())!=null){
                readLine = readLine.trim();
                if(readLine.matches("^[\\s&&[^\\n]]*$")){
                    whiteLine ++;
                }else if(readLine.startsWith("/*")&&!readLine.endsWith("*/")){
                    comentLine ++;
                    comPd = true;
                }else if(readLine.startsWith("/*")&&!readLine.endsWith("*/")){
                    comentLine ++;
                }else if(comPd){
                    comentLine ++;
                    if(readLine.endsWith("*/")){
                        comPd = false;
                    }
                }else if(readLine.startsWith("//")){
                    comentLine ++;
                }else{
                    sormaLine++;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
}
上一篇 下一篇

猜你喜欢

热点阅读