package com.syl.demo.test;

 import java.io.*;

 /**
* java代码行数统计工具类
* Created by 孙义朗 on 2017/11/17 0017.
*/
public class CountCodeLineUtil {
private static int normalLines = 0; //有效程序行数
private static int whiteLines = 0; //空白行数
private static int commentLines = 0; //注释行数 public static void countCodeLine(File file) {
System.out.println("代码行数统计:" + file.getAbsolutePath());
if (file.exists()) {
try {
scanFile(file);
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("文件不存在!");
System.exit(0);
}
System.out.println(file.getAbsolutePath() + " ,java文件统计:" +
"总有效代码行数: " + normalLines +
" ,总空白行数:" + whiteLines +
" ,总注释行数:" + commentLines +
" ,总行数:" + (normalLines + whiteLines + commentLines));
} private static void scanFile(File file) throws IOException {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
scanFile(files[i]);
}
}
if (file.isFile()) {
if (file.getName().endsWith(".java")) {
count(file);
}
}
} private static void count(File file) {
BufferedReader br = null;
// 判断此行是否为注释行
boolean comment = false;
int temp_whiteLines = 0;
int temp_commentLines = 0;
int temp_normalLines = 0; try {
br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.matches("^[//s&&[^//n]]*$")) {
// 空行
whiteLines++;
temp_whiteLines++;
} else if (line.startsWith("/*") && !line.endsWith("*/")) {
// 判断此行为"/*"开头的注释行
commentLines++;
comment = true;
} else if (comment == true && !line.endsWith("*/")) {
// 为多行注释中的一行(不是开头和结尾)
commentLines++;
temp_commentLines++;
} else if (comment == true && line.endsWith("*/")) {
// 为多行注释的结束行
commentLines++;
temp_commentLines++;
comment = false;
} else if (line.startsWith("//")) {
// 单行注释行
commentLines++;
temp_commentLines++;
} else {
// 正常代码行
normalLines++;
temp_normalLines++;
}
} System.out.println(file.getName() +
" ,有效行数" + temp_normalLines +
" ,空白行数" + temp_whiteLines +
" ,注释行数" + temp_commentLines +
" ,总行数" + (temp_normalLines + temp_whiteLines + temp_commentLines));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
} //测试
public static void main(String[] args) {
File file = new File("F:\\myweb");
countCodeLine(file);
}
}

java代码行数统计工具类的更多相关文章

  1. Python实现代码行数统计工具

    我们经常想要统计项目的代码行数,但是如果想统计功能比较完善可能就不是那么简单了, 今天我们来看一下如何用python来实现一个代码行统计工具. 思路:首先获取所有文件,然后统计每个文件中代码的行数,最 ...

  2. 【原】Mac下统计任意文件夹中代码行数的工具——cloc

    这里介绍一个Mac系统统计代码行数的工具cloc. 1.首先,安装homebrew,已安装的请跳过. 打开终端工具Terminal,输入下列命令.过程中会让你按RETURN键以及输入mac桌面密码,按 ...

  3. [转] 一句shell命令搞定代码行数统计

    今天面试时,突然被面试官问到怎样用shell命令搞定某个文件夹下java代码行数的统计. 想了一下,基本思路就是找到这个文件夹下面的所有java文件,然后每个文件统计一下代码,外层套个for循环,叠加 ...

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

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

  5. Android Studio代码行数统计插件Statistics

    Android Studio 是没有提提供统计代码全部行数的功能的,但是对于开发者来说,这个功能确实必备的,Statistic统计代码行数非常方便,也很详细. 1,首先肯定是将插件下载下来,下载地址: ...

  6. 代码行数统计(mac)

    如何统计自己的代码量?下面介绍两个方法: 一.cloc 1.首先,安装homebrew,已安装的请跳过. 打开终端工具Terminal,输入下列命令.过程中会让你按RETURN键以及输入mac桌面密码 ...

  7. Git代码行数统计命令

    统计zhangsan在某个时间段内的git新增删除代码行数 git log --author=zhangsan--since=2018-01-01 --until=2019-04-01 --forma ...

  8. 【转】VS 代码行数统计

    [转自]http://www.cnblogs.com/JuneZhang/archive/2013/01/10/2854572.html VS用正则表达式统计代码行数 利用VS2010的查找功能和正则 ...

  9. 代码行数统计的Java和Python实现

    通过编写程序来统计文件的行数,可以在巩固文件IO知识的同时计算出自己的代码量,以下分别提供Java和Python实现的版本. 解决思路 两种版本的思路几乎相同,每一个文件夹(目录)内的行数都是其所有子 ...

随机推荐

  1. Dev 之 GridControl 列表 显示底部(包括底部统计)

    1.列表 Gridview 显示底部 2 底部增加统计

  2. Python数据模型建立

    基本结构AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bigint自增列,必须填入参数 pri ...

  3. css3箭头

    <!DOCTYPE html> <html lang="en" class="muui-theme-webapp-main"> < ...

  4. 五,Smarty模板技术/引擎——自定义函数机制

    自建函数是smarty提供的函数,不允许修改,只能被调用: 自定义函数是自己编写函数,注册成为smarty的函数,之后可以被调用: 示例:使用smarty自定义函数的机制,编写一个函数myfun1,通 ...

  5. Elasticsearch 5.4.1 安装简易文档

    1. 系统安装JDK 1.8.0_131或以上版本的JDK 2. 创建软件安装目录 # mkdir -p /opt/software3. 创建elk用户,新版的ELK不在支持使用root账户运行程序, ...

  6. 扩大VirtualBox虚拟机磁盘的方法

    之前在VirtualBox里安装了一个XP系统,当时只分配了10G磁盘空间,随着使用,空间不足了. 在虚拟机管理器里不能直接调整磁盘的大小,这里要用到VirtualBox安装目录下的VBoxManag ...

  7. 搭建USB摄像头转RTSP服务器的多种方法

    USB摄像头与网络摄像头相比,可选择范围广.种类多.成本低,但是实际使用时需要通过rtsp流来访问,起到直播的效果,因此在摄像头采集终端上构建rtsp流媒体服务器,将USB摄像头数据转化为rtsp,可 ...

  8. nginx克隆之后问题

    [alert] kill(2942, 1) failed (No such process) 背景:虚拟机克隆之后启动service nginx reload报这个错,而且没有日志 根据这个博文htt ...

  9. 干掉Vivado幺蛾子(1)-- Xilinx Tcl Store

    目录 1. 安装Xilinx Tcl Store 2. 手动更新 2.1 下载库 2.2 修改环境变量 参考文献: 最近在跟着高亚军老师的分析文章来学习Xilinx最近发布的<UltraFast ...

  10. 主流服务器虚拟化技术简单使用——KVM(一)

    Tips:因为博客园排版的原因,图片显示不清晰,可以放大网页查看清晰图片. 如果系统使用物理机,需要在BIOS里面开启Intel VT-x(或AMD-V),如果是VMware workstation, ...