直接放在src/test/java包内运行

/**
* 代码行数统计
* @author ThinkGem
* @version 2014-7-22
*/
public class CodeCounter { public static void main(String[] args) {
//获取当前类的资源文件路径,即../../target/test-classes/
String file = CodeCounter.class.getResource("/").getFile();
// System.out.println(file);
//path=D:/Source_Code/koudaimini/src/
//设置统计根路径为src
// String path = file.replace("target/test-classes", "src");
String path = "D:\\Source_Code\\zbg\\modules\\zbg-api\\src"; ArrayList<File> al = CodeCounter.getFile(new File(path));
for (File f : al) {
if (f.getName().matches(".*\\.java$")){ // 匹配java格式的文件
CodeCounter.count(f);
System.out.println(f);
}
}
System.out.println("统计文件:" + CodeCounter.files);
System.out.println("代码行数:" + CodeCounter.codeLines);
System.out.println("注释行数:" + CodeCounter.commentLines);
System.out.println("空白行数:" + CodeCounter.blankLines);
}
static long files = 0;
static long codeLines = 0;
static long commentLines = 0;
static long blankLines = 0;
static ArrayList<File> fileArray = new ArrayList<File>(); /**
* 获得目录下的文件和子目录下的文件
* @param f
* @return
*/
public static ArrayList<File> getFile(File f) {
File[] ff = f.listFiles();
for (File child : ff) {
if (child.isDirectory()) {
CodeCounter.getFile(child);
} else {
CodeCounter.fileArray.add(child);
}
}
return CodeCounter.fileArray; } /**
* 统计方法
* @param f
*/
private static void count(File f) {
BufferedReader br = null;
boolean flag = false;
try {
br = new BufferedReader(new FileReader(f));
String line = "";
while ((line = br.readLine()) != null) {
line = line.trim(); // 除去注释前的空格
if (line.matches("^[ ]*$")) { // 匹配空行
CodeCounter.blankLines++;
} else if (line.startsWith("//")) {
CodeCounter.commentLines++;
} else if (line.startsWith("/*") && !line.endsWith("*/")) {
CodeCounter.commentLines++;
flag = true;
} else if (line.startsWith("/*") && line.endsWith("*/")) {
CodeCounter.commentLines++;
} else if (flag == true) {
CodeCounter.commentLines++;
if (line.endsWith("*/")) {
flag = false;
}
} else {
CodeCounter.codeLines++;
}
}
CodeCounter.files++;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

类似结果

D:/。。/koudaimini/src/
统计文件:46
代码行数:2798
注释行数:814
空白行数:652

javaWeb代码工程统计的更多相关文章

  1. [statsvn]-svn代码量统计

    用statasvn进行代码量统计的时候,第一步需要获取到项目的日志,但是我本机的svn1.4没有安装命令行,重新运行1.4的安装包也没有命令行的选项... 那就升级到最新的svn1.8好了,下载最新的 ...

  2. 如何创建 SVN 服务器,并搭建自己的 SVN 仓库 如何将代码工程添加到VisualSVN Server里面管理

    如何创建 SVN 服务器,并搭建自己的 SVN 仓库,附链接: https://jingyan.baidu.com/article/6b97984dca0d9c1ca3b0bf40.html 如何将代 ...

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

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

  4. IDEA 代码量统计(Statistic)

    IDEA 代码量统计(Statistic) 1.1 前言 项目到了一定阶段,都会想要看看项目的代码量情况,这里主要使用插件Statistic进行代码统计查看. 1.2 安装插件步骤 找到插件市场入口并 ...

  5. “代码量统计脚本”

    概述 本文从一段统计C/C++程序脚本入手,记录shell脚本常用和重要的知识点. 代码量统计程序 文件名称,count_code_line.sh 12345678910111213141516171 ...

  6. 代码图形统计工具git_stats web

    目录 一.简介 二.安装ruby 三.配置git_stats 四.通过nginx把网页展示出来 一.简介 仓库代码统计工具之一,可以按git提交人.提交次数.修改文件数.代码行数.注释量在时间维度上进 ...

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

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

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

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

  9. iOS纯代码工程手动快速适配

    首先说下让自己的程序支持iPhone6和6+,第一种使用官方提供的launch screen.xib,这个直接看官方文档即可,这里不再多述:第二种方法是和之前iPhone5的类似,比较简单,为iPho ...

随机推荐

  1. Python开发——基础

    注释 单行注释 # 被注释的内容 多行注释 """ 被注释的内容 """ 解释器路径 #!/usr/bin/env python # 用于L ...

  2. Difference Among Mercedes Star Diagnostic Tool MB Star C3 C4 C5 C6

    Mercedes Star Diagnostic Tool newly update to MB Star C6.There are many star diangostic tool in the ...

  3. C++ static 静态变量&静态成员函数

    .h文件中定义 static变量后,如 static QTcpSocket * socket; 那么一定要在.cpp中 构造函数的外面将其初始化为 QTcpSocket * Cfiletransfer ...

  4. shell 命令 if elif else fi 用法

    #! /bin/bash if Iam; then echo "it worked two" else ls echo "I am in the else" f ...

  5. 在ng-repeat内Checkbox默认选中

    Angularjs的ng-repeat是用来循环产生呈现数据. 当我们需要在ng-repeat循环中呈现一系列Checkbox时,某些checkbox选项是默认选中的. 在ASP.NET MVC程序中 ...

  6. cad.net cad启动慢? cad2008启动慢? cad启动延迟? cad卡住? cad98%卡? 默认打印机!!

    默认打印机是不是联网打印机,如果cad找不到这个打印机将会很慢才打开cad的界面(它真的不是卡死了,而是找不到...) 奇妙的是桌子至今都没有利用新建线程的方式来控制这个打印机等待,而是直接在主程序上 ...

  7. utf-8 decode

    摘自 Lua 5.3 源文件 lutf8lib.c /* ** Decode one UTF-8 sequence, returning NULL if byte sequence is invali ...

  8. POJ 1177Picture 扫描线(若干矩形叠加后周长)

    Picture   Description A number of rectangular posters, photographs and other pictures of the same sh ...

  9. LabVIEW(十四):VI属性

    1.VI的属性在项目研究中是很常用的,但是会经常忽略VI的属性设置,建议在编程事常常运用这些属性的设置,这样就可以使你的程序更加完善,易懂性也会提高. 属性快捷键:Ctrl+I. 2.常规 (1).编 ...

  10. Java 命令行启动时指定配置文件目录

    java -jar -Xbootclasspath/a:/home/tms/conf    /home/tms/bin/S17-tms.jar 先指定配置文件目录: 再指定jar包路径: 运行clas ...