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#编程(二十三)----------实现继承
原文链接:http://blog.csdn.net/shanyongxu/article/details/46593809 如果要声明派生自另一个类的一个类,可以使用下面的语法: class Deri ...
- Java 遍历类中的属性
public static void testReflect(Object model) throws NoSuchMethodException, IllegalAccessException, I ...
- 使PropertyGrid控件的属性值可以显示多行的方法
第一步:重写UITypeEditor的GetEditStyle方法: 第二部:重写UITypeEditor的EditValue方法: 具体实现如下: using System; using Syste ...
- 排序算法之归并排序(Mergesort)解析
转自:http://www.cnblogs.com/ayqy/p/4050452.html 一.归并排序的优缺点(pros and cons) 耗费心思来理解它,总要有个理由吧: 归并排序的效率达 ...
- Android之仿iphone抖动效果
转自:http://blog.csdn.net/long33long/article/details/7693671 布局文件: <?xml version="1.0" en ...
- vue elementui switch开关控件的使用
<el-switch @change="test" on-value="1" off-value="0" v-model=" ...
- Python获取数字的二进制值
目标 想要获取一个整形数字的二进制表示 bin 内置函数 看一下官方的解释 Convert an integer number to a binary string prefixed with &qu ...
- Chrome浏览器导出pdf时,隐藏链接HREF
在使用chrome打印pdf是,会出现链接的HREF也同时打印的情况,只要加一句CSS即可 @media print { a[href]:after { content: none !im ...
- IIS7增加mine类型,以便可以访问apk
1.打开IIS 2.找到mine类型,单击右边的添加 3.输入apk的配置 application/vnd.android-package-archive .这样,用户就可以直接访问apk了
- GDSL 1.7 发布,C语言通用数据结构库
GDSL 1.7 修复了 interval-heap 模块的一个小 bug. GDSL (通用数据结构库) 包含一组程序用于操作各种数据结构.这是一个可移植的库,完全由 ANSI C 编写.为 C 开 ...