统计指定的Java工程代码行数_统计JAVA项目的代码行数

统计JAVA项目的代码行数(包括注释、空行、java类数目)

http://iyanlei.com/count_java_lines.html

1.[代码][Java]代码

package com.demo.blog;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

/**

* 统计JAVA项目的代码行数(包括注释、空行、java类数目)

*

* @author ray

*

*/

public class SumJavaCode {

static long classcount = 0; // 类数

static long normalLines = 0; // 空行

static long commentLines = 0; // 注释行

static long wirteLines = 0; // 代码行

public static void main(String[] args) throws Exception {

File f = new File("C:\\Users\\lei\\workspace\\sqoop\\src\\java"); // 目录

SumJavaCode.treeFile(f);

System.out.println("路径:" + f.getPath());

System.out.println("类数:" + classcount);

System.out.println("空行:" + normalLines);

System.out.println("注释:" + commentLines);

System.out.println("代码:" + wirteLines);

System.out.println("平均:" + wirteLines / classcount);

}

/**

* 查找出一个目录下所有的.java文件

*

* @throws Exception

*/

public static void treeFile(File f) throws Exception {

File[] childs = f.listFiles();

for (int i = 0; i < childs.length; i++) {

File file = childs[i];

if (!file.isDirectory()) {

if (file.getName().endsWith(".java")) {

classcount++;

BufferedReader br = null;

boolean comment = false;

br = new BufferedReader(new FileReader(file));

String line = "";

while ((line = br.readLine()) != null) {

line = line.trim();

if (line.matches("^[//s&&[^//n]]*$")) {

wirteLines++;

} else if (line.startsWith("/*")

&& !line.endsWith("*/")) {

commentLines++;

comment = true;

} else if (true == comment) {

commentLines++;

if (line.endsWith("*/")) {

comment = false;

}

} else if (line.startsWith("//")) {

commentLines++;

} else {

normalLines++;

}

}

if (br != null) {

br.close();

br = null;

}

}

} else {

treeFile(childs[i]);

}

}

}

}

2.[代码]结果

sqoop代码行数结果:

类数:393

空行:29598

注释:14973

代码:6671

平均:16

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值