统计代码行数

public   class  ReadFile {
 
       /**
      *  @param  args
      *  @throws  IOException
      */
       public   static   void  main(String[] args)  throws  IOException {
           Long globalNum = 0L;  // 总行数
           List<Long> lineNums =  new  ArrayList<Long>();
           List<String> exceptFileNames =  new  ArrayList<String>();
           exceptFileNames.add(  "project2"  );//不统计目录
           exceptFileNames.add(  "test" ); //不统计目录
          String path =  "D:\\source\\project"  ;//项目目录
           File file =  new  File(path);
            traversalFile(file, lineNums, exceptFileNames);
             for  (Long num : lineNums) {
                globalNum = globalNum + num;
           }
           System.  out .println(globalNum);  // 310375
     }
 
       /**
      * 得到目录的叶子java文件,并统计文件里有多少行,将行数放到list中
      *
      *  @param  file
      *  @throws  IOException
      */
       public   static   void  traversalFile(File file, List<Long> lineNums,
                List<String> exceptFileNames)  throws  IOException {
           File[] childList = file.listFiles();
           String fileName =  null ;
           String extra =  null ;
             for  (File child : childList) {
                fileName = child.getName();
                  if  (child.isDirectory()) {
                       if  (exceptFileNames !=  null
                                && !exceptFileNames.contains(child.getName())) {
                            traversalFile(child, lineNums, exceptFileNames);
                     }
                }  else  {
                     extra = fileName.contains(  "." ) ? fileName.substring(fileName
                                .lastIndexOf(  "." )) : fileName;
                       if  (extra.equals(  ".java" ) || extra.equals( ".jsp"  )
                                || extra.equals(  ".js" )) {
                          System.  out .println(child.getName().substring(
                                     child.getName().lastIndexOf(  "." )));
                           lineNums.add( getLineNum(child));
                     }
                }
           }
     }
 
       /**
      * 得到某个文件有多少行(非目录)
      *
      *  @param  file
      *  @return
      *  @throws  IOException
      */
       public   static   long  getLineNum(File file)  throws  IOException {
             long  num = 0L;
           FileInputStream f =  new  FileInputStream(file);
           BufferedReader dr =  new  BufferedReader(  new  InputStreamReader(f));
           String line = dr.readLine();
             while  (line !=  null ) {
                  if  (!line.trim().equals(  "" )) {
                     num++;
                }
                line = dr.readLine();
           }
             return  num;
     }
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值