统计某目录下所有的java文件有效代码的行数

这个代码的功能是统计某个目录下的所有的java文件中的代码的行数
用到啦递归,还有少量的正则表达示,本来想看一下swing做一下图形界面
好看一些,但是自己方向不往那方面发展,还是算啦。写个中心的出来就行啦
有兴趣的可以看一下
java 代码
  1. import java.io.File;   
  2. import java.io.FileNotFoundException;   
  3. import java.io.BufferedReader;   
  4. import java.io.FileReader;   
  5. import java.io.IOException;   
  6.   
  7.   
  8. public class CountCodeLines   
  9. {   
  10.     static int codeLines=0;   
  11.     static int whiteLines=0;   
  12.     static int commentLines=0;   
  13.     static int tatolLines=0;   
  14.     static boolean bComment=false;   
  15.     public static void main(String[] args)    
  16.     {   
  17.         StringBuffer pathName=new StringBuffer("H:\\spring-framework-2.0.6\\src\\org\\springframework");   
  18.         ComputeDirectoryAndFiles(pathName,0);   
  19.         System.out.println("Code Lines : "+(codeLines=tatolLines-commentLines-whiteLines));   
  20.         System.out.println("White Lines : "+whiteLines);   
  21.         System.out.println("Comment Lines : "+commentLines);   
  22.   
  23.     }   
  24.   
  25.     public static void ComputeDirectoryAndFiles(StringBuffer pathName,int level){   
  26.         File directory=new File(pathName.toString());   
  27.         File[] files=directory.listFiles();   
  28.         String prefix="";   
  29.         for(int i=0;i<level;i++)   
  30.         {   
  31.             prefix+="** ";   
  32.         }   
  33.         if(directory.isDirectory()){   
  34.             for(int i=0;i<files.length;i++)   
  35.             {      
  36.                 if(files[i].isFile()&& files[i].getName().matches("^[a-zA-Z[^0-9]]\\w*.java$"))   
  37.                 {   
  38.                        
  39.                     computeLines(files[i]);   
  40.                 }   
  41.                 if(files[i].isDirectory())   
  42.                 {                  
  43.                        
  44.                     pathName.append("/"+files[i].getName());   
  45.                     level++;   
  46.                     ComputeDirectoryAndFiles(pathName,level);   
  47.                     int start=pathName.toString().length()-files[i].getName().length()-1;   
  48.                     int end=pathName.toString().length();   
  49.                     pathName.delete(start,end);                    
  50.                        
  51.                     level--;   
  52.                 }   
  53.             }   
  54.         }   
  55.     }   
  56.     public static void computeLines(File file)   
  57.     {      
  58.         BufferedReader bf=null;   
  59.            
  60.         try  
  61.         {   
  62.             bf=new BufferedReader(new FileReader(file));   
  63.             String lineStr="";   
  64.             while((lineStr=bf.readLine())!=null)   
  65.             {   
  66.                 //总行数   
  67.                 tatolLines++;   
  68.                 //计算空行   
  69.                 whiteLines(lineStr);   
  70.                 //统计代码行数   
  71.                 commendLines(lineStr);   
  72.                 //计算代码的行数   
  73.                 //codeLines(lineStr);   
  74.             }   
  75.         }   
  76.         catch (FileNotFoundException e)   
  77.         {   
  78.             System.out.println("文件没有找到");   
  79.         }catch(IOException ee)   
  80.         {   
  81.             System.out.println("输入输出异常 ");   
  82.         }finally  
  83.         {   
  84.             if(bf!=null)   
  85.             {   
  86.                 try{   
  87.                 bf.close();   
  88.                 bf=null;   
  89.                 }catch(Exception e)   
  90.                 {   
  91.                     System.out.println("关闭BufferReader时出错");   
  92.                 }   
  93.             }   
  94.         }   
  95.     }   
  96.     public static void whiteLines(String lineStr)   
  97.     {   
  98.         if(lineStr.matches("^[\\s&&[^\\n]]*$"))   
  99.         {   
  100.                     whiteLines++;   
  101.         }   
  102.   
  103.     }   
  104.     public static void commendLines(String lineStr)   
  105.     {   
  106.            
  107.                 //判断是否是一个注释行   
  108.                 //这里是单行注释的如 /*..... */或/**.... */   
  109.                 if(lineStr.matches("\\s*/\\*{1,}.*(\\*/).*"))   
  110.                 {   
  111.        
  112.                     commentLines++;   
  113.                 }   
  114.                 /**  
  115.                     这里是多行注释的  
  116.                 */  
  117.                 //这里的是当开始为/**或/*但是没有 */ 关闭时    
  118.                 else if(lineStr.matches("\\s*/\\*{1,}.*[^\\*/].*"))   
  119.                 {   
  120.                    
  121.                     commentLines++;   
  122.                     bComment=true;   
  123.                 }   
  124.                 else if(true==bComment)   
  125.                 {   
  126.                        
  127.                     commentLines++;   
  128.                     if(lineStr.matches("\\s*[\\*/]+\\s*"))   
  129.                     {   
  130.                         bComment=false;   
  131.                     }   
  132.                 }   
  133.                 else if(lineStr.matches("^[\\s]*//.*"))   
  134.                 {   
  135.            
  136.                     commentLines++;   
  137.                 }   
  138.   
  139.     }   
  140.     //public static void codeLines(String lineStr)   
  141.     //{   
  142.     //  if(lineStr.matches("\\s*[[^/\\*]|[//]]+.*"))   
  143.     //  codeLines++;   
  144.     //}   
  145. }  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值