java 代码量_java实现统计程序代码量

概述

作为一个programer 有的时候想统计一下自己写的代码量有多少呢,于是就写了一个小小的工具,统计某个目录的代码文件的行数,快来看看你的代码量上10w行了吗?

其实就是对文件目录进行递归操作,遇到文件(你要统计的代码文件后缀名),打开它统计行数。直到遍历完成

给出简单的代码

public class CountCode {

static String FileType=".cs";

public static void main(String[] args) {

File root=new File("c:\\");

countAllFiles(root);

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

}

private static void find(String rootFilePath,String fileName,int depth,int returnSize,boolean fuzzy,ArrayList returnFiles){

ArrayDeque filePathQueue=new ArrayDeque();

filePathQueue.add(rootFilePath);

String []fileList;

String currentFilePath;

String temp;

int depthCount=1;

while(!filePathQueue.isEmpty()){

currentFilePath=filePathQueue.remove();

fileList=new File(currentFilePath).list();

if(fileList!=null){

for(int i=0;i

temp=currentFilePath+"\\"+fileList[i];

if(compare(fileList[i], fileName, fuzzy)){

returnFiles.add(temp);

}

if(returnFiles.size()==returnSize){

break;

}

if(!new File(temp).isFile()){

filePathQueue.add(temp);

}

}

}

}

if(depthCount++==depth){

return;

}

}

private static boolean compare(String currentFileName,String fileName,boolean fuzzy){

if(currentFileName.contains(fileName)){

return true;

}else{

return false;

}

}

static int allcount=0;

final static void countAllFiles(File dir){

File[] fs = dir.listFiles();

for(int i=0; i

{

if(fs[i].isDirectory())

{

try

{

countAllFiles(fs[i]);

}

catch(Exception e){}

}else

{

if(fs[i].getName().endsWith(FileType))

{

System.out.println(fs[i].getAbsolutePath());

allcount+=count(fs[i]);

}

}

}

}

public static int count(File file)

{

int count=0;

try {

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

String tempString = null;

while ((tempString = reader.readLine()) != null)

{

count++;

}

reader.close();

} catch (Exception e) {

return count;

}

return count;

}

}

运行结果截图

0818b9ca8b590ca3270a3433284dd417.png

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值