package cn;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
/**
* @author yeqv
* @program A2
* @Classname test
* @Date 2022/1/16 15:20
* @Email w16638771062@163.com
*/
public class test {
static int num = 0;
static int rows = 0;
static int space = 0;
public static void main(String[] args) throws FileNotFoundException {
String path = "D:\\idea\\ja";
count(new File("D:\\idea\\ja"));
System.out.printf("%s 共有%d个java程序文件%n", "d:/eclipse- workspace", num);
System.out.printf("共 %d 行代码%n", rows);
System.out.printf("共 %d 空行代码%n", space);
}
public static void count(File dir) throws FileNotFoundException {
if (dir.isDirectory()) {
File[] f = dir.listFiles();
for (File t : f) {
if (t.isDirectory()) count(t);
if (t.isFile() && t.getName().endsWith(".java")) {
++num;
BufferedReader br = new BufferedReader(new FileReader(t));
rows += br.lines().filter(e -> e.trim().length() > 0).count();
BufferedReader br2 = new BufferedReader(new FileReader(t));
space += br2.lines().filter(e -> e.trim().length() == 0).count();
}
}
}
if (dir.isFile() && dir.getName().endsWith(".rar")) {
++num;
BufferedReader br = new BufferedReader(new FileReader(dir));
rows += br.lines().count();
BufferedReader br2 = new BufferedReader(new FileReader(dir));
space += br2.lines().filter(e -> e.trim().length() == 0).count();
}
}
}
读取某个目录下所有JAVA文件的代码行数
最新推荐文章于 2022-09-22 08:40:02 发布