java统计数量程序,Java 编写程序统计项目文件总数(可指定后缀名)、文件行数(可去除空白行和注释)...

Java 编写程序统计项目文件总数(可指定后缀名)、文件行数(可去除空白行和注释)

发布时间:2018-09-19作者:laosun阅读(1227)

0f9e8264915948f7a1174b4a48c40c63.gif

Java 编写程序统计项目文件总数(可指定后缀名)、文件行数(可去除空白行和注释)

直接看源码吧,没什么好解释的。package com.sunjs.demo;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

/**

* Java代码统计 [指定后缀的文件总数、文件内行数、文件内空白行数、文件内注释行数]

* @author sun

*/

public class CodeCounterDemo {

private static Integer codeLine = 0;

private static Integer commentsLine = 0;

private static Integer blankLine = 0;

private static Integer fileCount = 0;

private static final String PATH = "/Users/sun/Documents/workspace/xxx";//检索目录

private static final List suffixList = new ArrayList<>();//需要统计的文件名后缀

static{

suffixList.add("java");

//suffixList.add("xml");

}

public static void main(String[] args) {

long s = System.currentTimeMillis();

File file = new File(PATH);

getAllFile(file);

System.out.println("文件总数:" + fileCount);

System.out.println("代码行数:" + codeLine);

System.out.println("空白行数:" + blankLine);

System.out.println("注释行数:" + commentsLine);

System.out.println("总行数:" + (codeLine + blankLine + commentsLine));

long e = System.currentTimeMillis();

System.out.println("\n总耗时:"+(e-s));

}

public static void getAllFile(File file) {

BufferedReader br = null;

String s = null;

if (file.isDirectory()) {

File[] files = file.listFiles();

for (File f : files) {

getAllFile(f);

}

} else {

if(file.getName().indexOf(".")!=-1){

//if(file.getName().equals("pom.xml")){

//System.out.println();

//}

//System.out.println(file.getName());

String suffix = file.getName().substring(file.getName().lastIndexOf(".") + 1);

if(suffixList.contains(suffix)){

fileCount++;

try {

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

boolean comm = false;

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

if (s.startsWith("/*") && s.endsWith("*/")) {

commentsLine++;

} else if (s.trim().startsWith("//")) {

commentsLine++;

} else if (s.startsWith("/*") && !s.endsWith("*/")) {

commentsLine++;

comm = true;

} else if (!s.startsWith("/*") && s.endsWith("*/")) {

commentsLine++;

comm = false;

} else if (comm) {

commentsLine++;

} else if (s.trim().length() 

blankLine++;

} else {

codeLine++;

}

}

br.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

}

输出结果:

文件总数:2340

代码行数:745719

空白行数:143001

注释行数:49551

总行数:938271

总耗时:1369

af499b9437efec8e1b25c2bb396e60d7.png

3 +1

版权声明

分享到:

发表评论

请文明留言

发表

共 0 条评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值