代码行数统计工具

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: 焦一平
 * Date: 2015/6/3
 * Time: 15:56
 * To change this template use File | Settings | File Templates.
 */
public class CountLineNumber {
    static long commentLine = 0;
    static long whiteLine = 0;
    static long normalLine = 0;
    static long totalLine = 0;
    static boolean comment = false;

    public static void main(String[] args) {
        File file = new File("D:\\工作\\PDS\\trunk\\sourcecode\\service\\pns"); // 在这里输入需要统计的文件夹路径
        countLine(file);
        System.out.println("有效代码行数: " + normalLine);
        System.out.println("注释行数: " + commentLine);
        System.out.println("空白行数: " + whiteLine);
        System.out.println("总代码行数: " + totalLine);
    }

    /**
     * 递归统计代码行数
     *
     * @param child
     */
    private static void countLine(File child) { // 遍历子目录
        if (child.getName().matches(".*\\.java$") || child.getName().matches(".*\\.xml$") || child.getName().matches(".*\\.properties") || child.getName().matches(".*\\.json")) { // 只查询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()) {
                countLine(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 (isComment(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 (isComment(line)) {
                comment = false;
            } else {
                comment = true;
            }
        } else if (line.matches(".+/\\*.*") && line.endsWith("*/")) {
            commentLine++;
            normalLine++;
            comment = false;
        } else {
            normalLine++;
        }
    }

    private static boolean isComment(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);
    }

}
时间: 2024-08-25 20:22:58

代码行数统计工具的相关文章

推荐一个代码行数统计工具cloc

代码行数统计工具cloc,它不但能统计代码行数,还能自动过滤掉代码中的注释,使用起来也很方便,强力推荐. 安装很方便,Ubuntu上直接udo apt-get install cloc就可以了 下面用mysql来做个例子吧,首先进入mysql的源代码文件夹: [email protected] ~/aproject $ cd mysql-5.6.19/ [email protected] ~/aproject/mysql-5.6.19 $ cloc . defined(%hash) is dep

Python实现代码行数统计工具

我们经常想要统计项目的代码行数,但是如果想统计功能比较完善可能就不是那么简单了, 今天我们来看一下如何用python来实现一个代码行统计工具. 思路:首先获取所有文件,然后统计每个文件中代码的行数,最后将行数相加. 实现的功能: 统计每个文件的行数: 统计总行数: 统计运行时间: 支持指定统计文件类型,排除不想统计的文件类型: 递归统计文件夹下包括子文件件下的文件的行数: 排除空行: # coding=utf-8 import os import time basedir = '/root/sc

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

这里介绍一个Mac系统统计代码行数的工具cloc. 1.首先,安装homebrew,已安装的请跳过. 打开终端工具Terminal,输入下列命令.过程中会让你按RETURN键以及输入mac桌面密码,按照提示进行操作即可: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 2.利用homebrew下载并安装cloc. 继续在Terminal中输入: brew

代码行数统计

/** * Copyright ? 2015 All rights reserved. */ package cn.yufu.system.tools; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList;

Android Studio代码行数统计插件Statistics

Android Studio 是没有提提供统计代码全部行数的功能的,但是对于开发者来说,这个功能确实必备的,Statistic统计代码行数非常方便,也很详细. 1,首先肯定是将插件下载下来,下载地址:https://plugins.jetbrains.com/plugin/4509 2,下载下来之后我们将插件安装到AS上面: 进入设置界面Setting之后点击plugins,如下图所示,显示的是已经安装的插件名称,我们将下载的插件安装,点击下面按钮: 点击之后,选择已经下载好的plugins插件

代码行数统计(mac)

如何统计自己的代码量?下面介绍两个方法: 一.cloc 1.首先,安装homebrew,已安装的请跳过. 打开终端工具Terminal,输入下列命令.过程中会让你按RETURN键以及输入mac桌面密码,按照提示进行操作即可: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 这里需要注意的是ruturn键即是enter键 2.利用homebrew下载并安装c

文件行数和代码行数统计

统计当前目录下,py文件数量:    find . -name "*.py" |wc -l统计当前目录下,所有py文件行数:    find . -name "*.py" |xargs cat|wc -l统计当前目录下,所有py文件行数,并过滤空行:    find . -name "*.py" |xargs cat|grep -v ^$|wc -l 统计某文件夹下文件的个数 ls -l |grep "^-"|wc -l 统计

xcode代码行数统计

进入终端 cd 路径 进入项目根目录 输入下面的命令,显示总行数(不包含空行,包括注释和应用第三方类) find . -name "*.m" -or -name "*.h" -or -name "*.xib" -or -name "*.c" |xargs grep -v "^$"|wc -l 输入下面的命令,显示各个文件行数以及总行数(包含空行,包括注释和应用第三方类) find . -name "

Visual Studio 中粗略的代码行数统计

在VS中, Ctrl + Shift +  F 调出查询窗, 输入正则表达式, 勾选"Use Regular Expressions", 选择好目标的文件类型, 点击Find All 自VS2012版本后: ^(?([^\r\n])\s)*[^\s+?/]+[^\n]*$ (?([^\r\n])\s)  代表any white space character ^\s+?/ 代表以/开头的行,即注释行 ^\n  代表行结尾 VS2012之前: ^:b*[^:b#/]+.*$ MSDN上关