分享统计Java web项目的行数的代码

可以自动统计Java项目中后缀为.java,.js,.css文件的行数,空行自动忽略

main方法中传入Java项目的绝对路径即可


[java]  view plain  copy
  1. package com.bjfl.electric.lottery.test;  
  2. import java.io.BufferedReader;  
  3. import java.io.File;  
  4. import java.io.FileReader;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.    
  8.   
  9. public class LineCounter {  
  10.     List<File> list = new ArrayList<File>();  
  11.     int linenumber = 0;  
  12.        
  13.     FileReader fr = null;  
  14.     BufferedReader br = null;  
  15.    
  16.     public void counter(String path) {  
  17. //        String path = System.getProperty("user.dir");  
  18.         System.out.println("项目路径"+path);  
  19.         File file = new File(path);  
  20.         File files[] = null;  
  21.         files = file.listFiles();  
  22.         addFile(files);  
  23.         isDirectory(files);  
  24.         readLinePerFile();  
  25.         System.out.println("Totle:" + linenumber + "行");  
  26.     }  
  27.    
  28.     // 判断是否是目录  
  29.     public void isDirectory(File[] files) {  
  30.         for (File s : files) {  
  31.             if (s.isDirectory()) {  
  32.                 File file[] = s.listFiles();  
  33.                 addFile(file);  
  34.                 isDirectory(file);  
  35.                 continue;  
  36.             }  
  37.         }  
  38.     }  
  39.    
  40.     //将src下所有文件组织成list  
  41.     public void addFile(File file[]) {  
  42.         for (int index = 0; index < file.length; index++) {  
  43.             list.add(file[index]);  
  44.             // System.out.println(list.size());  
  45.         }  
  46.     }  
  47.        
  48.     //读取非空白行  
  49.     public void readLinePerFile() {  
  50.         try {  
  51.             for (File s : list) {  
  52.                 int yuan = linenumber;  
  53.                 if (s.isDirectory()) {  
  54.                     continue;  
  55.                 }  
  56.                 if(s.getName().lastIndexOf(".java")>0||s.getName().lastIndexOf(".jsp")>0||s.getName().lastIndexOf(".js")>0||s.getName().lastIndexOf(".css")>0)  
  57.                 {  
  58.                       
  59.                 }else{  
  60.                     continue;  
  61.                 }  
  62.                 fr = new FileReader(s);  
  63.                 br = new BufferedReader(fr);  
  64.                 String i = "";  
  65.                 while ((i = br.readLine()) != null) {  
  66.                     if (isBlankLine(i))  
  67.                         linenumber++;  
  68.                 }  
  69.                 System.out.print(s.getName());  
  70.                 System.out.println("\t\t有" + (linenumber - yuan) + "行");  
  71.             }  
  72.         } catch (Exception e) {  
  73.             e.printStackTrace();  
  74.         } finally {  
  75.             if (br != null) {  
  76.                 try {  
  77.                     br.close();  
  78.                 } catch (Exception e) {  
  79.                 }  
  80.             }  
  81.             if (fr != null) {  
  82.                 try {  
  83.                     fr.close();  
  84.                 } catch (Exception e) {  
  85.                 }  
  86.             }  
  87.         }  
  88.     }  
  89.    
  90.     //是否是空行  
  91.     public boolean isBlankLine(String i) {  
  92.         if (i.trim().length() == 0) {  
  93.             return false;  
  94.         } else {  
  95.             return true;  
  96.         }  
  97.     }  
  98.        
  99.     public static void main(String args[]) {  
  100.         LineCounter lc = new LineCounter();  
  101.         lc.counter("W:/workspaces/wm/xxProject"); //$NON-NLS-1$  
  102.     }  
  103. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值