利用正则表达式获取特定文件(如java class)并对其进行处理(如代码统计)
2008-10-15 15:36
836 查看
这是一个利用正则表达式获取特定文件(如java class)并对其进行处理(如代码统计)的程序。可以在特定的指定的文件目录下寻找指定的文件,在下面程序里主要是寻找java文件并进行数量统计和代码统计(代码行、空行、注释行)。至于删除那些class文件主要是因为我统计的是我下载的JDK的源码,里面的class文件不知道什么时候产生的,手动删除实在麻烦。在我的机器上的结果是:StatisticalJavaFiles:7070
CodeLine:894658
ComentLine:901352
WhiteLine :238595
呵呵,Sun公司发布的Java源码有90万行,不算多。估计没公布的比公布的还多。而且统计出来的结果中注释的语句比代码行还多。这也充分说明了注释得重要性。
/*
* StatisticalCodes.java 2008-10-15
* This program was write for find out some kinds of
* file that we specify.
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* @author Sancho_lai
*/
public class StatisticalCodes {
//codes line count
private static long codeLines = 0;
//comment line count
private static long commentLines = 0;
//white line count
private static long whiteLines = 0;
//number of java files was found
private static long statisticalJavaFiles = 0;
//input a directory that you want,but it will cost a NullPointException when you input a <root directory>
private static String path = "D://jdkSrc";
public static void main(String[] args) {
File file = new File(path);
findSpecifiedDoc(file);
System.out.println("StatisticalJavaFiles:" + statisticalJavaFiles);
System.out.println("CodeLine:" + codeLines);
System.out.println("ComentLine:" + commentLines);
System.out.println("WhiteLine :" + whiteLines);
}
/**
* find all the files that fit for the Regular Expression you specify
* and then deal with it and count the lines and delete the class files
* you can extend it you want to be
*/
private static void findSpecifiedDoc(File file) {
File[] fileArray = file.listFiles();
for(File child:fileArray) {
if(child.isDirectory()) { //when child was a directory recursively call <findSpecifiedDoc> until get java file
findSpecifiedDoc(child);
}else if(child.getName().matches(".*//.java$")) { //when the <child> was a java file ,count the lines
parser(child);
statisticalJavaFiles++;
}else if(child.getName().matches(".*//.class$")){ //when the <child> was a class file , delete it
deleteClassFile(child);
}
}
}
/**
* delete the class type file
* @param child
*/
private static void deleteClassFile(File child) {
child.delete();
}
/**
* parser the java file count the codeLines ...
* using the regular expression for judging
* @param child
*/
private static void parser(File child) {
BufferedReader br = null;
boolean comment = false;
String line = "";
try {
br = new BufferedReader(new FileReader(child));
while((line = br.readLine()) != null) {
line = line.trim();
if(line.matches("^[//s&&[^//n]]*$")) { //
whiteLines ++;
} else if (line.startsWith("/*") && !line.endsWith("*/")) {//comment lines start with /*
commentLines ++;
comment = true;
} else if (line.startsWith("/*") && line.endsWith("*/")) {
commentLines ++;
} else if (true == comment) {
commentLines ++;
if(line.endsWith("*/")) {
comment = false;
}
} else if (line.startsWith("//")) {
commentLines ++;
} else { //it suppose that the java files just contain three kinds of lines
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();
}
}
}
}
}
CodeLine:894658
ComentLine:901352
WhiteLine :238595
呵呵,Sun公司发布的Java源码有90万行,不算多。估计没公布的比公布的还多。而且统计出来的结果中注释的语句比代码行还多。这也充分说明了注释得重要性。
/*
* StatisticalCodes.java 2008-10-15
* This program was write for find out some kinds of
* file that we specify.
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* @author Sancho_lai
*/
public class StatisticalCodes {
//codes line count
private static long codeLines = 0;
//comment line count
private static long commentLines = 0;
//white line count
private static long whiteLines = 0;
//number of java files was found
private static long statisticalJavaFiles = 0;
//input a directory that you want,but it will cost a NullPointException when you input a <root directory>
private static String path = "D://jdkSrc";
public static void main(String[] args) {
File file = new File(path);
findSpecifiedDoc(file);
System.out.println("StatisticalJavaFiles:" + statisticalJavaFiles);
System.out.println("CodeLine:" + codeLines);
System.out.println("ComentLine:" + commentLines);
System.out.println("WhiteLine :" + whiteLines);
}
/**
* find all the files that fit for the Regular Expression you specify
* and then deal with it and count the lines and delete the class files
* you can extend it you want to be
*/
private static void findSpecifiedDoc(File file) {
File[] fileArray = file.listFiles();
for(File child:fileArray) {
if(child.isDirectory()) { //when child was a directory recursively call <findSpecifiedDoc> until get java file
findSpecifiedDoc(child);
}else if(child.getName().matches(".*//.java$")) { //when the <child> was a java file ,count the lines
parser(child);
statisticalJavaFiles++;
}else if(child.getName().matches(".*//.class$")){ //when the <child> was a class file , delete it
deleteClassFile(child);
}
}
}
/**
* delete the class type file
* @param child
*/
private static void deleteClassFile(File child) {
child.delete();
}
/**
* parser the java file count the codeLines ...
* using the regular expression for judging
* @param child
*/
private static void parser(File child) {
BufferedReader br = null;
boolean comment = false;
String line = "";
try {
br = new BufferedReader(new FileReader(child));
while((line = br.readLine()) != null) {
line = line.trim();
if(line.matches("^[//s&&[^//n]]*$")) { //
whiteLines ++;
} else if (line.startsWith("/*") && !line.endsWith("*/")) {//comment lines start with /*
commentLines ++;
comment = true;
} else if (line.startsWith("/*") && line.endsWith("*/")) {
commentLines ++;
} else if (true == comment) {
commentLines ++;
if(line.endsWith("*/")) {
comment = false;
}
} else if (line.startsWith("//")) {
commentLines ++;
} else { //it suppose that the java files just contain three kinds of lines
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();
}
}
}
}
}
相关文章推荐
- 利用正则表达式统计某个某个文件夹下所有java文件的 空格行数、注释行数、和代码行数
- 统计*.Java文件代码行数——正则表达式
- 利用正则表达式统计代码中的(代码行数,注释行数,空白行数)利用正则表达式获取一个网页中所有的邮箱地址
- hdfs获取文件列表,利用正则表达式选择文件并作下一步处理
- 正则表达式 统计java文件代码的数量
- 代码笔记 | java 正则表达式高级篇(四种常用的处理方式:匹配 分割 替代 获取)
- JAVA学习代码——利用正则表达式判断手机/邮箱/身份证
- java读取txt文件,使用正则表达式获取信息
- Java利用正则表达式统计某个字符串出现的次数
- 在Idea中利用正则表达式找出匹配行并捕获内容进行处理
- java 正则表达式2 通过正则表达式统计代码行数
- java 正则表达式高级篇(四种常用的处理方式:匹配 分割 替代 获取)
- Java利用正则表达式统计某个字符串出现的次数
- [Java] 正则表达式 02 EmailSpider 抓邮件, CodeCounter 统计程序有效代码行数
- Java读取文件及基于正则表达式的获取电话号码功能详解
- 利用Visual Studio的搜索功能和正则表达式统计代码行数
- Java利用正则表达式统计某个字符串出现的次数
- java 正则表达式高级篇(四种常用的处理方式:匹配 分割 替代 获取)
- Java利用正则表达式统计某个字符串出现的次数
- 采用正则表达式获取文本文件中的特定模式字符串