JAVA里getDataCount_Java 统计所有文件的行数

package com.ec.xy.app.algorithm;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Objects;

import com.xiuye.sharp.X;

import com.xiuye.util.log.XLog;

public class CodeStatistics {

static class FileTravel {

private long fileCount = 0l;

private long dirCount = 0l;

private long lineCount = 0l;

private long codeLine = 0l;

public long getLineCount() {

return lineCount;

}

public void setLineCount(long lineCount) {

this.lineCount = lineCount;

}

public long getCodeLine() {

return codeLine;

}

public void setCodeLine(long codeLine) {

this.codeLine = codeLine;

}

public long getFileCount() {

return fileCount;

}

public void setFileCount(long fileCount) {

this.fileCount = fileCount;

}

public long getDirCount() {

return dirCount;

}

public void setDirCount(long dirCount) {

this.dirCount = dirCount;

}

private void scanFile(File f) {

try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));) {

String line;

do {

line = br.readLine();

//XLog.ln(line);

lineCount++;

X.of(line).E(d -> {

//if (!d.isEmpty()) {

//codeLine++;

//}

X.of(d.isEmpty()).F(g->{

codeLine++;

});

});

} while (Objects.nonNull(line = br.readLine()));

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public void travel(String path) {

File f = new File(path);

X.of(f.exists()).T(d -> {

X.beginS().IF(f.isDirectory()).THEN(i -> {

dirCount++;

//XLog.ln("dir:", f);

File[] fs = f.listFiles();

for (File ff : fs) {

//X.lnS(ff);

travel(ff.getAbsolutePath());

}

}).ELSE(j -> {

fileCount++;

//XLog.ln("file:", f);

scanFile(f);

}).end();

});

}

}

public static void main(String[] args) {

FileTravel ft = new FileTravel();

ft.travel("G:\\projects\\mybatis-plus");

X.lnS("扫描文件夹数:",ft.getDirCount(),"\n",

"扫描文件数:",ft.getFileCount(), "\n",

"扫描文件总行数:",ft.getLineCount(), "\n",

"扫描文件总代码行数:",ft.getCodeLine());

}

}

扫描文件夹数: 725

扫描文件数: 1697

扫描文件总行数: 170458

扫描文件总代码行数: 165644 [ line:111 | com.ec.xy.app.algorithm.CodeStatistics | CodeStatistics.java ]

maven依赖:

com.github.xiuyexye

xyjava

0.0.20

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值