Java文件扫描
2010-06-07 12:58
155 查看
package com.yhj.common.counter; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import com.yhj.common.io.FileUtil; /** * com.yhj.common.counter.CodeCounter.java * be created by 2009-12-10 下午05:21:31+ * 本类的主要功能: * <p> * 统计某个文件夹下文件 Java文件 以及 * 统计指定目录下以及其子目录下的所有java文件中代码行数 注释数 空行数等 * </p> * @author 一线天色 天宇星辰 * */ public class CodeCounter { static long codeLines = 0; //代码行数 static long commentLines = 0; //注释行署 static long blankLines = 0; //空白行数 static long totalFileCount = 0; //总共文件数目 static long totalFolderCount = 0;//文件夹文件数目 static long javaFileCount = 0; //Java文件数目 static boolean isOne=false; //将每次生成的文件生成到一个文件中的标示 static ArrayList<File> fileArray = new ArrayList<File>(); private static String fileName=null; /** * 可以统计指定目录下以及其子目录下的所有java文件中代码 */ public static void codeCounter(String path) { //统计程序运行时间 long beginTime = System.currentTimeMillis(); //统计运行消耗的内存 long beginMemory = Runtime.getRuntime().freeMemory(); //要统计的文件夹 File file = new File(path); SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String beginDetailTime=simpleDateFormat.format(new Date()); System.out.println("====================================="); writeToLog("====================================="); System.out.println("开始扫描文件夹 "+path+"..."); writeToLog("开始扫描文件夹 "+path+"..."); //存储文件的数据 ArrayList<File> al = getFile(file); System.out.println("====================================="); writeToLog("====================================="); System.out.println("开始统计文件..."); writeToLog("开始统计文件..."); for (File f : al) { if (f.getName().matches(".*\\.java$")) // 匹配java格式的文件 { System.out.println("正在统计文件:"+f.getPath()); writeToLog("正在统计文件:"+f.getPath()); javaFileCount++; count(f); } } System.out.println("完成统计!"); writeToLog("完成统计!"); System.out.println("====================================="); writeToLog("====================================="); System.out.println("统计结果如下:"); writeToLog("统计结果如下:"); System.out.println("文件夹总数:" + totalFolderCount+" 个"); writeToLog("文件夹总数:" + totalFolderCount+" 个"); System.out.println("文件总个数:" + totalFileCount+" 个"); writeToLog("文件总个数:" + totalFileCount+" 个"); System.out.println("Java文件数:" + javaFileCount+" 个"); writeToLog("Java文件数:" + javaFileCount+" 个"); System.out.println("代码行数: " + codeLines); writeToLog("代码行数:" + codeLines); System.out.println("注释行数: " + commentLines); writeToLog("注释行数:" + commentLines); System.out.println("空白行数: " + blankLines); writeToLog("空白行数: " + blankLines); long endMemory = Runtime.getRuntime().freeMemory(); long billMemory=beginMemory-endMemory; long endTime = System.currentTimeMillis(); long sec=(endTime-beginTime)/1000; System.out.println("程序开始时间:"+beginDetailTime); writeToLog("程序开始时间:"+beginDetailTime); String endDetailTime=simpleDateFormat.format(new Date()); System.out.println("程序结束时间:"+endDetailTime); writeToLog("程序结束时间:"+endDetailTime); System.out.println("程序运行共耗时间:"+sec/60+"分"+sec%60+"秒"); writeToLog("程序运行共耗时间:"+sec/60+"分"+sec%60+"秒"); System.out.println("程序运行共耗内存:"+billMemory*1.0/1024+"KB"); writeToLog("程序运行共耗内存:"+billMemory*1.0/1024+"KB"); } // 获得目录下的文件和子目录下的文件 private static ArrayList<File> getFile(File f) { System.out.println("开始扫描子文件夹"+f.getPath()+"..."); writeToLog("开始扫描子文件夹"+f.getPath()+"..."); File[] ff = f.listFiles(); for (File child : ff) { if (child.isDirectory()) { System.out.println("发现文件夹:"+child.getPath()); writeToLog("发现文件夹:"+child.getPath()); totalFolderCount++; getFile(child); } else { fileArray.add(child); totalFileCount++; System.out.println("发现文件:"+child.getPath()); writeToLog("发现文件:"+child.getPath()); } } System.out.println("文件夹"+f.getPath()+"扫描结束!"); writeToLog("文件夹"+f.getPath()+"扫描结束!"); return fileArray; } // 统计文件 private static void count(File f) { BufferedReader br = null; boolean flag = false; try { br = new BufferedReader(new FileReader(f)); String line = ""; while ((line = br.readLine()) != null) { line = line.trim(); // 除去注释前的空格 if (line.matches("^[ ]*$")) { // 匹配空行 blankLines++; } else if (line.startsWith("//")) { commentLines++; } else if (line.startsWith("/*") && !line.endsWith("*/")) { commentLines++; flag = true; } else if (line.startsWith("/*") && line.endsWith("*/")) { commentLines++; } else if (flag == true) { commentLines++; if (line.endsWith("*/")) { flag = false; } } else { codeLines++; } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); br = null; } catch (IOException e) { e.printStackTrace(); } } } } private static void writeToLog(String data) { try { if(!isOne) { SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH.mm.ss"); fileName=simpleDateFormat.format(new Date())+".log"; isOne=true; } FileUtil.writeStringToFile("F:/YHJ日志生成区",fileName, data+"\n", "UTF-8",true); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { codeCounter("C:/Program Files"); } }
相关文章推荐
- 使用apache common-io 定时监控文件下文件的变化,java文件扫描文件变化
- Android下c++调用java实现内置外置sd卡MP3扫描并保存在文件中
- java 是扫描计算机的全部文件
- android 5.0 ,zbar扫描java.lang.UnsatisfiedLinkError,找不到so文件
- 用java扫描指定文件夹下面所有以.txt, .log结尾的文件,
- JAVA扫描指定目录下指定关键字文件
- 扫描配置文件(二)Java 读写Properties配置文件
- java扫描文件夹下面的所有文件(递归与非递归实现)
- 用JAVA扫描指定文件夹下面所有以.txt结尾的文件,并将其绝对路径输出 .
- Java扫描指定文件路径下的文件并且递归扫描其子目录下的所有文件
- JNI实例3---扫描SD卡中mp3文件,native层调用Java自定义的类
- 用JAVA扫描指定文件夹下面所有以.txt结尾的文件,并将其绝对路径输出
- 一个Java文件扫描 统计程序
- Java 实现 扫描两文件内容 是否包含。
- JNI实例2---扫描SD卡中mp3文件,native层调用Java自定义的类
- java扫描源码或者jar包中的class文件
- 利用AST扫描java文件 获得 类中的函数列表
- Java 扫描指定包下的文件
- java 扫描Class文件获取接口对应的实现类及其方法util
- Java编写敏感文件扫描工具