Java 代码行统计(转)
package codecounter; 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 CodeCounter {
static long commentLine = 0;
static long whiteLine = 0;
static long normalLine = 0;
static long totalLine = 0;
static boolean comment = false;
static final String base_dir = "D:\\javap\\qdgs\\qdgs_gzfw\\src\\java\\qdgsgzfw"; public static void main(String[] args) {
File file = new File(base_dir); // 在这里输入需要统计的文件夹路径 getChild(file); System.out.println("有效代码行数: " + normalLine);
System.out.println("注释行数: " + commentLine);
System.out.println("空白行数: " + whiteLine);
System.out.println("总代码行数: " + totalLine);
} private static void getChild(File child) { // 遍历子目录
if (child.getName().matches(".*\\.java$")) { // 只查询java文件
try {
BufferedReader br = new BufferedReader(new FileReader(child));
String line = "";
while ((line = br.readLine()) != null) {
parse(line);
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if (child.listFiles() != null) {
for (File f : child.listFiles()) {
getChild(f);
}
}
} private static void parse(String line) {
line = line.trim();
totalLine++;
if (line.length() == 0) {
whiteLine++;
} else if (comment) {
commentLine++;
if (line.endsWith("*/")) {
comment = false;
} else if (line.matches(".*\\*/.+")) {
normalLine++;
comment = false;
}
} else if (line.startsWith("//")) {
commentLine++;
} else if (line.matches(".+//.*")) {
commentLine++;
normalLine++;
} else if (line.startsWith("/*") && line.matches(".+\\*/.+")) {
commentLine++;
normalLine++;
if (findPair(line)) {
comment = false;
} else {
comment = true;
}
} else if (line.startsWith("/*") && !line.endsWith("*/")) {
commentLine++;
comment = true;
} else if (line.startsWith("/*") && line.endsWith("*/")) {
commentLine++;
comment = false;
} else if (line.matches(".+/\\*.*") && !line.endsWith("*/")) {
commentLine++;
normalLine++;
if (findPair(line)) {
comment = false;
} else {
comment = true;
}
} else if (line.matches(".+/\\*.*") && line.endsWith("*/")) {
commentLine++;
normalLine++;
comment = false;
} else {
normalLine++;
}
} private static boolean findPair(String line) { // 查找一行中/*与*/是否成对出现
int count1 = 0;
int count2 = 0;
Pattern p = Pattern.compile("/\\*");
Matcher m = p.matcher(line);
while (m.find()) {
count1++;
}
p = Pattern.compile("\\*/");
m = p.matcher(line);
while (m.find()) {
count2++;
}
return (count1 == count2);
} }
Java 代码行统计(转)的更多相关文章
- java代码行数统计工具类
package com.syl.demo.test; import java.io.*; /** * java代码行数统计工具类 * Created by 孙义朗 on 2017/11/17 0017 ...
- 【转】Git 代码行统计命令集
查看git上个人代码量 git log --author="username" --pretty=tformat: --numstat | awk '{ add += $1; su ...
- 统计文件夹下java代码行数的小程序--主要是学习任务队列的思想
首先感谢czbk的老师,录制的视频,让我们有这么好的学习资料.……—— 统计文件夹java文件的行数,首先想到的肯定是用递归的方法,因为文件夹下面可能包含文件夹,用递归的方法,代码容易写.(这和写简单 ...
- 【转】Git代码行统计命令集
http://blog.csdn.NET/dwarven/article/details/46550117 http://blog.csdn.net/hshl1214/article/details/ ...
- 基于Gitlab统计代码行--统计所有仓库、所有提交人的代码总行数(新增加-删除)
公司绩效考核要求,统计GITLAB仓库所有人提示有效代码行业 脚本1: 统计所有仓库.所有提交人的代码总行数(新增加-删除) 脚本2: 统计所有仓库.所有提交人的代码提交汇总与删除汇总 脚本3: 统计 ...
- Git代码行统计命令集
统计某人的代码提交量,包括增加,删除: git log --author="$(git config --get user.name)" --pretty=tformat: --n ...
- mac OS X下git代码行统计命令
1.统计某人的代码提交量,包括增加,删除 git log --author=-- --until=-- --pretty=tformat: --numstat | awk '{ add += $1 ; ...
- java代码行数计算器
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util. ...
- XCODE 代码行统计
find . -name "*.m" -or -name "*.h" -or -name "*.c" |xargs grep -v &quo ...
随机推荐
- C#高级编程小结
小结 这几章主要介绍了如何使用新的dynamic类型,还讨论了编译器在遇到dynamic类型时会做什么.还讨论了DLP,可以把它包含在简单的应用程序中.并通过Pythin使用DLR,执行Python脚 ...
- cocos2d-x调用scheduleUpdate()不执行update()方法的解决办法
前两天使用到每帧都更新动画的scheduleUpdate()方法,但通过cclog,我发现, scheduleUpdate()是执行了.但update()方法并没有被调用. 那是因为在CCLayer中 ...
- 通过泛型来简化findViewById
我们一般写findViewById都要加个强制转换,感觉很麻烦,现在你可以在你的BaseActivity中写入如下方法: @SuppressWarnings(“unchecked”) public f ...
- Orchard 之:Widget,兼看 Layer 在权限控制中的作用
一:Widget 可以理解为控件,可以直接被页面所引用.行为类似与分部页面,比如,我们可以创建一个 商品列表 Widget,然后这个 Widget 就可以被很多页面所引用. 理解 Widget 这个概 ...
- Docker 常用命令与操作
介绍 此命令集合版本为 1.11.1 及以上 基础类 查看docker信息 # 查看docker版本 docker version # 显示docker系统的信息 docker info # 日志信息 ...
- [转]php判断一个数组是另一个数组的子集
FROM : http://blog.csdn.net/lcion/article/details/8985220 今天完成一个算法的过程中,有几个需求模块,其中就有判断$a数组是否是$b数组的子集, ...
- Intelli公司IDE快捷键设置
自定义快捷键:Keymap 搜索某个像自定义的快捷键,看被占用了没有:搜索时,直接键盘敲击快捷键,比如command+T
- uva 400 Unix ls 文件输出排版 排序题
这题的需要注意的地方就是计算行数与列数,以及输出的控制. 题目要求每一列都要有能够容纳最长文件名的空间,两列之间要留两个空格,每一行不能超过60. 简单计算下即可. 输出时我用循环输出空格来解决对齐的 ...
- Solr4.4入门,介绍Solr的安装、IK分词器的配置及高亮查询结果(转)
一.Windows下安装solr-4.4.0 1. 下载solr.4.4 2. 下载绿色版tomcat6.0.18 3. 解压下载的solr到d:\study\solr,将dist目录下的sol ...
- JQuery-Dialog(弹出窗口,遮蔽窗口)
在Ajax中经常用到的弹出窗口和遮蔽窗口.自己写肯定是一个最佳方案,但时间和成本上,还是决定了寻找现成的吧.大概罗列一下.需要我满足我几个条件 一定要简洁方便 拥有遮蔽功能,Model Dialog ...