源代码计算器

  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileReader;  
  6. import java.io.IOException;  
  7. import java.io.Reader;  
  8. import java.util.regex.Matcher;  
  9. import java.util.regex.Pattern;  
  10. //***import块实现了字符串的输入***  
  11.   
  12. public class Code {  
  13.   
  14.     @SuppressWarnings("unused")  
  15.     private static final String PROJECT_DIR = "D:\\eclipse";  /**此句有待探讨*/  
  16.     //声明文件路径  
  17.     private static int totle = 0;       //总行数                 
  18.     private static int source  = 0;     //代码行数            
  19.     private static int blank  = 0;      //空白行数                
  20.     private static int comments = 0;    //注释行数  
  21.     //private 块实现的是声明了totle;source;blank;comments;这些私有静态的对象  
  22.     /** 
  23.      * 读取文件夹内java文件 
  24.      *  
  25.      */  
  26.     private static void listNext(File dir) {   
  27.         //一个静态方法  
  28.         File[] files = dir.listFiles();  
  29.         //file 对象定义了文件夹内寻找,作用就是从当前所有文件和文件夹中过滤出所有文件夹  
  30.         for (int i = 0; i < files.length; i++) {  
  31.             //判断是否是文件夹,如果是文件夹继续向下检索  
  32.             if (files[i].isDirectory()) {  
  33.                 listNext(files[i]);  
  34.             } else {  
  35.                 try {  
  36.                     if (files[i].getName().endsWith(".java")) {  
  37.                         System.out.println(files[i].getAbsolutePath());  
  38.                         javaLine(files[i]);  
  39.                     }  
  40.                 }catch (Exception e) { //捕获异常  
  41.                     e.printStackTrace();  
  42.                 }  
  43.             }  
  44.         }  
  45.     }  
  46.   
  47.     /** 
  48.      * 读取java文件总行数,代码行数,空白行数,注释行数 
  49.      */  
  50.     private static void javaLine(File f) throws FileNotFoundException, IOException{  
  51.         //通过调用一个之前定义的对象来表示此文件连接,file参数表示的路径作为参数调用javaLine方法  
  52.         String strLine = "";//创建了一个对象。并且加入字符串池中  
  53.         String str = fromFile(f);  
  54.         //str为引用  "" 是 引用指向的的值,定义一个String 类型的变量str,并为其赋值  
  55.         if (str.length() > 0) {  
  56.             while (str.indexOf('\n') != -1) {   
  57.                 //通过一个wile循环体判断这个值,从而做到字符串处理,使得字符串以正确方式显示  
  58.                 totle++;  
  59.   
  60.                 strLine = str.substring(0, str.indexOf('\n')).trim();  
  61.   
  62.                 if (strLine.length() == 0 ) {  
  63.                     blank++;  
  64.                 }else if (strLine.charAt(0) == '*' || strLine.charAt(0) == '/') {  
  65.                     comments++;  
  66.                 }else{  
  67.                     source++;  
  68.                     String regEx = "^*//";  
  69.                     if(regEx(strLine,regEx)){   
  70.                         comments++;  
  71.                     }  
  72.                 }  
  73.                 str = str.substring(str.indexOf('\n') + 1, str.length());  
  74.                 //返回给定区间的字符串,以上经过转换后,str能够正常显示  
  75.             }  
  76.         }     
  77.     }  
  78.   
  79.     /** 
  80.      * 将java文件以字符数组形式读取 
  81.      */  
  82.     private static String  fromFile(File f) throws FileNotFoundException,IOException {  
  83.         FileInputStream fis = new FileInputStream(f);  
  84.         //  FileInputStream类的重载,读取当前目录下的java文件  
  85.         byte[] b = new byte[(int) f.length()];  
  86.         //用f的长度初始化一个byte型数组;对返回的长度值做强制类型转换,转换成int型  
  87.         fis.read(b);  
  88.         fis.close();  
  89.         return new String(b);//就是返回一个b的实例  
  90.     }  
  91.   
  92.     /** 
  93.      * 输入字符串 
  94.      * 正则匹配字符串 
  95.      */  
  96.     private static boolean regEx(String str,String regEx){  
  97.         //如果包含正则表达式,则定义特殊的字符,regex匹配的字符串  
  98.         Pattern p=Pattern.compile(regEx);//pattern类的实例表示以字符形式指定的正则表达式,此处是一个编译成某种模式  
  99.         Matcher m=p.matcher(str);//创建一个匹配器  
  100.         boolean result=m.find();  
  101.         return result;  
  102.     }  
  103.     public static void main(String[] args) throws FileNotFoundException, IOException {  
  104.         File directory = new File("");//参数为空   
  105.         //获取项目路径  
  106.         String projectFile = directory.getCanonicalPath() ;  
  107.         System.out.println(projectFile+"********");  
  108.         listNext(new File(projectFile));  
  109.         System.out.println("文件总行数:"+totle+1);  
  110.         System.out.println("代码行数:"+source);  
  111.         System.out.println("空白行数:"+blank);  
  112.         System.out.println("注释行数:"+comments);  
  113.     }  
  114. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值