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. Android项目更换开发环境时出现的 java.lang.VerifyError 异常解决办法

    from://http://blog.csdn.net/wudiwo/article/details/7548451 项目是从同事的电脑上直接拷贝过来的,项目里面的jar包是在项目跟下libs里面存放 ...

  2. skb的两个函数pskb_copy和skb_copy

    转自:http://blog.csdn.net/farmwang/article/details/54235252 skb的两个函数pskb_copy和skb_copy 前者仅仅是将sk_buff的结 ...

  3. lemon OA 我长时间经历的第一个开源项目

    对于原作者来说, 他长时间运营了一个项目,lemon OA .目前,八百多star.在运营这个项目的过程中,我想说,他成了activiti 目前国内比较牛逼的几个人.还有 spring securit ...

  4. jquery 图片文件转base64 显示

    <!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...

  5. cocos2d-x 3.0 WIN7+VS2012 安卓平台搭建

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  6. 深入理解多线程(二)—— Java的对象模型

    上一篇文章中简单介绍过synchronized关键字的方式,其中,同步代码块使用monitorenter和monitorexit两个指令实现,同步方法使用ACC_SYNCHRONIZED标记符实现.后 ...

  7. Generalized normal distribution and Skew normal distribution

    Density Function The Generalized Gaussian density has the following form: where  (rho) is the " ...

  8. 部署Percona XtraDB Cluster高可用和多Master集群

    http://www.it165.net/admin/html/201401/2306.html http://www.oschina.net/p/percona-xtradb-cluster/ ht ...

  9. C# Encoding UTF-16 ,C#中的UTF16

    http://www.cnblogs.com/criedshy/archive/2012/08/07/2625358.html 前言 众所周知计算机只能识别二进制数字,如1010,1001.我们屏幕所 ...

  10. 【BZOJ】【4145】【AMPPZ2014】The Prices

    状压DP/01背包 Orz Gromah 容易发现m的范围很小……只有16,那么就可以状压,用一个二进制数来表示买了的物品的集合. 一种简单直接的想法是:令$f[i][j]$表示前$i$个商店买了状态 ...