利用正则表达式编写的代码统计工具
2015-12-30 01:20
344 查看
package regexp; import java.io.BufferedReader; import java.io.File; import java.io.FilePermission; import java.io.FileReader; import java.text.Normalizer; public class countline { static long normalLines=0; static long commentLines=0; static long whiteLines=0; public static void main(String[] args) { // TODO Auto-generated method stub File f=new File("F:\\src\\java\\dao\\impl");//要统计的代码存放的所在的文件夹路径 File[] codeFiles=f.listFiles(); for(File child: codeFiles){ if(child.getName().matches(".*\\.java$")){ parse(child); } } System.out.println("normalLines:"+normalLines); System.out.println("commentLines:"+commentLines); System.out.println("whiteLines:"+whiteLines); } private static void parse(File f) { BufferedReader bReader=null; boolean comment=false; try { bReader=new BufferedReader(new FileReader(f)); String line=" "; while((line=bReader.readLine())!=null) { line=line.trim(); if(line.matches("^[\\s&&[^\\n]]*$")){ whiteLines++; }else if(line.startsWith("/*")&&!line.endsWith("*/")){ commentLines++; comment=true; }else if(line.startsWith("/*")&&line.endsWith("*/")){ commentLines++; } else if(true==comment){ commentLines++; if(line.endsWith("*/")){ comment=false; } }else if(line.endsWith("//")){ commentLines++; } else { normalLines++; } } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ if(bReader!=null){ try { bReader.close();bReader=null; } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } } } } }
相关文章推荐
- msql 正则表达式
- 正则表达式
- Mootools 1.2教程 正则表达式
- 批处理FINDSTR正则表达式用法实例分析
- vbs正则表达式代码
- C#正则表达式Regex类的常用匹配
- C#使用正则表达式实现首字母转大写的方法
- php中看实例学正则表达式
- PHP正则表达式之定界符和原子介绍
- PHP下常用正则表达式整理
- 批处理 正则表达式(findstr) 整理
- 详解C#正则表达式Regex常用匹配
- C#正则表达式的6个简单例子
- JavaScript类型系统之正则表达式
- javascript中的正则表达式使用指南
- C#中的正则表达式介绍
- 学习正则帮手笨狼正则练习器
- java正则表达式应用的实例代码
- ruby 正则表达式 教程