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 代码行统计(转)的更多相关文章

  1. java代码行数统计工具类

    package com.syl.demo.test; import java.io.*; /** * java代码行数统计工具类 * Created by 孙义朗 on 2017/11/17 0017 ...

  2. 【转】Git 代码行统计命令集

    查看git上个人代码量 git log --author="username" --pretty=tformat: --numstat | awk '{ add += $1; su ...

  3. 统计文件夹下java代码行数的小程序--主要是学习任务队列的思想

    首先感谢czbk的老师,录制的视频,让我们有这么好的学习资料.……—— 统计文件夹java文件的行数,首先想到的肯定是用递归的方法,因为文件夹下面可能包含文件夹,用递归的方法,代码容易写.(这和写简单 ...

  4. 【转】Git代码行统计命令集

    http://blog.csdn.NET/dwarven/article/details/46550117 http://blog.csdn.net/hshl1214/article/details/ ...

  5. 基于Gitlab统计代码行--统计所有仓库、所有提交人的代码总行数(新增加-删除)

    公司绩效考核要求,统计GITLAB仓库所有人提示有效代码行业 脚本1: 统计所有仓库.所有提交人的代码总行数(新增加-删除) 脚本2: 统计所有仓库.所有提交人的代码提交汇总与删除汇总 脚本3: 统计 ...

  6. Git代码行统计命令集

    统计某人的代码提交量,包括增加,删除: git log --author="$(git config --get user.name)" --pretty=tformat: --n ...

  7. mac OS X下git代码行统计命令

    1.统计某人的代码提交量,包括增加,删除 git log --author=-- --until=-- --pretty=tformat: --numstat | awk '{ add += $1 ; ...

  8. java代码行数计算器

        import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util. ...

  9. XCODE 代码行统计

    find . -name "*.m" -or -name "*.h" -or -name "*.c" |xargs grep -v &quo ...

随机推荐

  1. css 滚动条样式

    1,Overflow内容溢出时的设置 overflow 水平及垂直方向内容溢出时的设置 overflow-x 水平方向内容溢出时的设置 overflow-y 垂直方向内容溢出时的设置 以上三个属性设置 ...

  2. 异步图片下载引擎(升级版——ExecutorService+handler)

    [Android分享] 异步图片下载引擎(升级版——ExecutorService+handler)  [复制链接]     皮诺 13 主题 5 好友 844 积分 No.4 中级开发者 升级  2 ...

  3. python测试开发django-40.模型(model)中choices使用

    前言 之前一直在想页面上如果一个字段只有固定的几个选项,类似select下拉框这种,如果在表里面设置一个外键的话,是不是有点傻了,这样为了几个选项弄一张表不值得. 后来看到Django模型中的字段有个 ...

  4. (转载):ASCII,Unicode和UTF-8 编码

    UTF-8是Unicode的一种实现方式,也就是它的字节结构有特殊要求,所以我们说一个汉字的范围是0X4E00到0x9FA5,是指unicode值,至于放在utf-8的编码里去就是由三个字节来组织,所 ...

  5. Fix "Drives are running out of free space" Error in SharePoint Health Analyzer

    前言 最近帮助用户做健康检查,用户发现事件查看器(EventView)里面有很多错误,有一个就是"Drives are running out of free space",而且每 ...

  6. 毒枭第一季/全集Narcos迅雷下载

    英文全名Narcos,第1季(2015)Netflix.本季看点:<缉毒特警>该剧再现了上世纪八十年代末期警方与臭名昭著的大毒枭激烈交战的真实故事.律师.法官.政客.警察.军队甚至平民全都 ...

  7. 用开源项目SwitchButton实现各种风格的switch

    今天介绍的开源项目是否的优秀,又是国人的作品.之前我接触过很多很多的自定义switch,有些动画僵硬,有些不能自定义switch的宽度,有些只能定义宽度不能设置滑块的宽高.但,这个项目提供了各种定制的 ...

  8. 离线环境下使用二进制方式安装配置Kubernetes集群

    本文环境 Redhat Linux 7.3,操作系统采用的最小安装方式. Kubernetes的版本为 V1.10. Docker版本为18.03.1-ce. etcd 版本为 V3.3.8. 1. ...

  9. hive php连接查询

    baidu hive php PHP连接Hive执行sql查询 php通过 thrift访问hadoop的hive php开发Hive Web查询 php连接hive执行sql查询 利用python将 ...

  10. js转义和反转义html htmlencode htmldecode

    文章目录 JS实现HTML标签转义及反转义 用Javascript进行HTML转义 1.HTML转义 2.反转义 3.一个有意思的认识 4.完整版本的代码 其他 [转义字符]HTML 字符实体< ...