利用正则表达式实现统计代码中的注释行、空行、以及代码行的数量。
2013-08-18 15:43
821 查看
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ConvertString1 {
static int normalLines = 0;
static int commentLines = 0;
static int whiteLines = 0;
public static void main(String[] args) {
// 读取文件中的.java 文件
File f = new File(
"D:\\作业\\范例\\图书进存销系统\\book\\src\\org\\crazyit\\book\\ui");
File[] files = f.listFiles();
for (File file : files) {
// 正则表达式匹配
String s = file.toString();
Pattern p = Pattern.compile(".*\\.java$");
Matcher m = p.matcher(s);
while (m.find()) {
parse(s);
}
}
// 输出统计结果
System.out.println("normalLines=" + normalLines + "," + "commentLines="
+ commentLines + "," + "whiteLines=" + whiteLines);
}
private static void parse(String name) {
BufferedReader br = null;
String str = "";
boolean flag = false;
try {
br = new BufferedReader(new FileReader(name));
while ((str = br.readLine()) != null) {
str=str.trim();
// 判断是否为空行
if (str.matches("^[\\s&&[^\\n]]*$")) {
whiteLines++;
// 判断是否注释为/*……*/
} else if (str.startsWith("/*") && (!str.endsWith("*/"))) {
commentLines++;
flag = true;
}
// 判断是否注释行为"//"开头
else if (str.startsWith("//")) {
commentLines++;
}
else if (true==flag) {
commentLines++;
if (str.endsWith("*/")) {
flag=false;
}
}else {
// 普通代码行
normalLines++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ConvertString1 {
static int normalLines = 0;
static int commentLines = 0;
static int whiteLines = 0;
public static void main(String[] args) {
// 读取文件中的.java 文件
File f = new File(
"D:\\作业\\范例\\图书进存销系统\\book\\src\\org\\crazyit\\book\\ui");
File[] files = f.listFiles();
for (File file : files) {
// 正则表达式匹配
String s = file.toString();
Pattern p = Pattern.compile(".*\\.java$");
Matcher m = p.matcher(s);
while (m.find()) {
parse(s);
}
}
// 输出统计结果
System.out.println("normalLines=" + normalLines + "," + "commentLines="
+ commentLines + "," + "whiteLines=" + whiteLines);
}
private static void parse(String name) {
BufferedReader br = null;
String str = "";
boolean flag = false;
try {
br = new BufferedReader(new FileReader(name));
while ((str = br.readLine()) != null) {
str=str.trim();
// 判断是否为空行
if (str.matches("^[\\s&&[^\\n]]*$")) {
whiteLines++;
// 判断是否注释为/*……*/
} else if (str.startsWith("/*") && (!str.endsWith("*/"))) {
commentLines++;
flag = true;
}
// 判断是否注释行为"//"开头
else if (str.startsWith("//")) {
commentLines++;
}
else if (true==flag) {
commentLines++;
if (str.endsWith("*/")) {
flag=false;
}
}else {
// 普通代码行
normalLines++;
}
}
} 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源文件中注释、代码、空白行数量)
- 利用正则表达式统计某个某个文件夹下所有java文件的 空格行数、注释行数、和代码行数
- 利用正则表达式统计代码中的(代码行数,注释行数,空白行数)利用正则表达式获取一个网页中所有的邮箱地址
- 利用正则表达式实现去除所有HTML标签代码
- php利用正则表达式实现手机号码格式验证代码
- 利用正则表达式获取特定文件(如java class)并对其进行处理(如代码统计)
- java正则表达式例程 提取网页中的email 统计代码的空行 有效代码行 注释行
- 利用PYTHON的正则表达式去掉代码中的注释
- 正则表达式 统计java文件代码的数量
- 利用JavaScript和正则表达式进行丰富的日期判断(给其它项目组的代码,有比较好的编程风格和注释)
- vs利用正则表达式统计代码行数
- [转]利用正则表达式,统计代码行数。
- 去掉代码中的空行和注释行,可以使用以下正则表达式替换。
- 利用Visual Studio的搜索功能和正则表达式统计代码行数
- @V@ java代码笔记2010-06-12:java控制台输入各类型类实现;以及判断输入字符串里面是否有数字的两种方法:方法1:转换成字符数组;方法2:正则表达式。
- [转]利用正则表达式,统计代码行数。
- 关于代码中空白、注释等字符的统计(正则表达式的使用)
- Python实现代码统计(代码+注释+空行)
- 关于代码中空白、注释等字符的统计(正则表达式的使用)
- 优酷电视剧爬虫代码实现一:下载解析视频网站页面(4)补充: Java正则表达式Matcher.group(int group)相关类解析