一个Java文件扫描 统计程序
2014-09-01 10:26
309 查看
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"); } }
相关文章推荐
- 一个使用多线程删除指定目录及子目录下所有指定文件的Java程序(源码)
- 一个使用JAVA编写的类似按键精灵的程序,支持脚本文件编写(含源代码)
- 练习2-3:编一个程序用来打开文件并统计文件中以空格隔开的单词数目
- 一个java文件操作小程序 system.out.in buffer read
- 复制一个文件夹中的所有文件和文件夹的java程序实现
- 一个java写的文件查看程序
- 使用.bat文件运行Java程序的一个示例
- 利用IO的知识,实现一个程序列出某个目录下的所有的”.java”文件。
- 练习2-4:编一个程序统计文件中特定单词出现的次数(要求使用string类的运算符==来查找单词) .
- 用IO流编写一个程序,统计并输出某个文本文件中“a”字符的个数
- 实战Registry和RegistryKey类,一个简单的可疑文件扫描程序
- Java程序运行结果输出到一个文件中去,结果显示乱码
- 一个使用JAVA编写的类似按键精灵的程序,支持脚本文件编写(含源代码)
- 练习2-4:编一个程序统计文件中特定单词出现的次数(要求使用string类的运算符==来查找单词)
- 给定一个Java文件,统计文件中的代码行
- 实战Registry和RegistryKey类,一个简单的可疑文件扫描程序
- 一个使用JAVA编写的类似按键精灵的程序,支持脚本文件编写(含源代码)
- 一个使用JAVA编写的类似按键精灵的程序,支持脚本文件编写(含源代码)
- 一个java实现的简单文件下载程序
- 如何用java程序直接打开一个Excel文件.