java 正则表达式2 通过正则表达式统计代码行数
2014-06-15 14:15
706 查看
import java.io.*; import java.util.regex.*; import java.util.Scanner; public class CodeCounter{ public static int whiteCount = 0; //空白行 public static int normalCount = 0; //代码行 public static int commentCount = 0; //注释行 public static void main(String []args){ String filePath = ""; String endString = ""; Scanner scanner= new Scanner(System.in); System.out.println("请输入您要统计的文件目录,或者文件名,分隔符为/"); filePath = scanner.nextLine(); File file = new File(filePath); System.out.println("请输入一个您要统计的以什么为结尾的文件如: java"); endString = scanner.nextLine(); if(null == file){ System.out.println("打开文件失败,请查看路径是否正确"); return ; } if(file.isFile()){ //如果只是一个标准文件 if(file.getName().matches(".*\\."+endString+"$")){ parse(file); } } else if(file.isDirectory()){//如果是一个目录 File[] childs = file.listFiles(); getCount(childs,endString); } /*if(file.isFile()){ //如果只是一个标准文件 if(file.getName().matches(".*\\."+endString+"$")){ parse(file); } } else if(file.isDirectory()){//如果是一个目录 File[] childs = file.listFiles(); System.out.println(childs.length); for(File child :childs){ if(child.getName().matches(".*\\."+endString+"$")){ parse(child); } } }*/ System.out.println("空白行:"+whiteCount); System.out.println("代码行:"+normalCount); System.out.println("注释行:"+commentCount); } public static void getCount(File[]files,String endString){ for(File file :files){ if(file.isFile()){ //如果只是一个标准文件 if(file.getName().matches(".*\\."+endString+"$")){ parse(file); } } else if(file.isDirectory()){//如果是一个目录 File[] childs = file.listFiles(); getCount(childs,endString); } } } private static void parse(File f){ BufferedReader br = null; String line = ""; boolean comment = false; //用来标识注释的开始于结束 try{ br = new BufferedReader(new FileReader(f)); while((line = br.readLine())!=null){ //该方法会去除结尾的\n line = line.trim(); if(line.matches("^[\\t&&[^\\n]]*$")){ whiteCount++; }else if(line.startsWith("/*")&&!line.endsWith("*/")){ commentCount++; comment = true; }else if (true == comment){ commentCount++; if(line.endsWith("*/")){ comment = false; } } else if(line.startsWith("//")){ commentCount++; } else{ normalCount++; } } }catch (Exception e){ e.printStackTrace(); }finally{ try{ br.close(); br = null; }catch(Exception e){ e.printStackTrace(); } } } }
相关文章推荐
- 统计*.Java文件代码行数——正则表达式
- [Java] 正则表达式 02 EmailSpider 抓邮件, CodeCounter 统计程序有效代码行数
- 利用正则表达式统计某个某个文件夹下所有java文件的 空格行数、注释行数、和代码行数
- [Java] 正则表达式 02 EmailSpider 抓邮件, CodeCounter 统计程序有效代码行数
- [转]利用正则表达式,统计代码行数。
- vs2005用正则表达式统计有效代码行数
- 通过Java正则表达式去掉SQL代码中回车换行和多余空格
- 正则表达式 统计java文件代码的数量
- vs利用正则表达式统计代码行数
- 通过Java正则表达式去掉SQL代码中回车换行和多余空格
- 利用Visual Studio的搜索功能和正则表达式统计代码行数
- 利用正则表达式统计代码中的(代码行数,注释行数,空白行数)利用正则表达式获取一个网页中所有的邮箱地址
- Java正则表达式(三)、代码量统计工具(统计java源文件中注释、代码、空白行数量)
- java正则表达式例程 提取网页中的email 统计代码的空行 有效代码行 注释行
- [转]利用正则表达式,统计代码行数。
- vs2005用正则表达式统计有效代码行数
- 利用正则表达式获取特定文件(如java class)并对其进行处理(如代码统计)
- VS用正则表达式统计代码行数
- Visual Studio中使用正则表达式快速统计项目总共代码行数
- 通过代码学习C#&.NET——委托使用(正则表达式替换)