java代码行数统计工具

[java]  view plain copy
  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileReader;  
  5. import java.io.IOException;  
  6.   
  7. /** 
  8.  * 代码行数统计 
  9.  */  
  10. public class StatisticCodeLines {  
  11.       
  12.     public static int normalLines = 0;  //有效程序行数  
  13.     public static int whiteLines = 0;   //空白行数  
  14.     public static int commentLines = 0//注释行数  
  15.       
  16.     public static void main(String[] args) throws IOException{  
  17.           
  18.         File file = new File("d://workspace//project");  
  19.         if (file.exists()) {  
  20.             statistic(file);  
  21.         }  
  22.         System.out.println("总有效代码行数: " + normalLines);  
  23.         System.out.println("总空白行数:" + whiteLines);  
  24.         System.out.println("总注释行数:" + commentLines);  
  25.         System.out.println("总行数:" + (normalLines + whiteLines + commentLines));  
  26.     }  
  27.       
  28.     private static void statistic(File file) throws IOException {  
  29.   
  30.         if (file.isDirectory()) {  
  31.             File[] files = file.listFiles();  
  32.             for (int i = 0; i < files.length; i++) {  
  33.                 statistic(files[i]);  
  34.             }  
  35.         }  
  36.         if (file.isFile()) {  
  37.             //统计扩展名为java的文件  
  38.             if(file.getName().matches(".*//.java")){  
  39.                 parse(file);  
  40.             }  
  41.         }  
  42.   
  43.     }  
  44.       
  45.     public static void parse(File file) {  
  46.         BufferedReader br = null;  
  47.         // 判断此行是否为注释行  
  48.         boolean comment = false;  
  49.         int temp_whiteLines = 0;  
  50.         int temp_commentLines = 0;  
  51.         int temp_normalLines = 0;  
  52.           
  53.         try {  
  54.             br = new BufferedReader(new FileReader(file));  
  55.             String line = "";  
  56.             while ((line = br.readLine()) != null) {  
  57.                 line = line.trim();  
  58.                 if (line.matches("^[//s&&[^//n]]*$")) {  
  59.                     // 空行  
  60.                     whiteLines++;  
  61.                     temp_whiteLines++;  
  62.                 } else if (line.startsWith("/*") && !line.endsWith("*/")) {  
  63.                     // 判断此行为"/*"开头的注释行  
  64.                     commentLines++;  
  65.                     comment = true;  
  66.                 } else if (comment == true && !line.endsWith("*/")) {  
  67.                     // 为多行注释中的一行(不是开头和结尾)  
  68.                     commentLines++;  
  69.                     temp_commentLines++;  
  70.                 } else if (comment == true && line.endsWith("*/")) {  
  71.                     // 为多行注释的结束行  
  72.                     commentLines++;  
  73.                     temp_commentLines++;  
  74.                     comment = false;  
  75.                 } else if (line.startsWith("//")) {  
  76.                     // 单行注释行  
  77.                     commentLines++;  
  78.                     temp_commentLines++;  
  79.                 } else {  
  80.                     // 正常代码行  
  81.                     normalLines++;  
  82.                     temp_normalLines++;  
  83.                 }  
  84.             }  
  85.               
  86.             System.out.println("有效行数" + temp_normalLines +   
  87.                              " ,空白行数" + temp_whiteLines +   
  88.                              " ,注释行数" + temp_commentLines +   
  89.                              " ,总行数" + (temp_normalLines + temp_whiteLines + temp_commentLines) +   
  90.                              "     " + file.getName());  
  91.               
  92.         } catch (FileNotFoundException e) {  
  93.             e.printStackTrace();  
  94.         } catch (IOException e) {  
  95.             e.printStackTrace();  
  96.         } finally {  
  97.             if (br != null) {  
  98.                 try {  
  99.                     br.close();  
  100.                     br = null;  
  101.                 } catch (IOException e) {  
  102.                     e.printStackTrace();  
  103.                 }  
  104.             }  
  105.         }  
  106.     }  
  107.       
  108. }  
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贺佬湿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值