import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
/**
* @ClassName: YY
* @Author: LW
* @Date: 2021-01-29 17:54
**/
public class NumberOfCodes{
public static void main(String[] args) throws IOException {
//行数
int row=0;
//个数
int count=0;
codeNumber("G:\\Demo代码\\Y2s", "xxx", "");
i = 0;
j = 0;
codeNumber("F:\\IdeaWork\\Game", "xxxx", "");
row += i;
count +=j;
i = 0;
j = 0;
codeNumber("F:\\IdeaWork\\MSE", "xxx", "");
row += i;
count +=j;
System.out.println("项目代码文件个数:"+count);
System.out.println("项目代码总行数:"+row);
}
public static int i, j;
public static void codeNumber(String path, String countTxt, String numberTxt) throws IOException {
// int i=0;//代码总行数
// int j=0;//文件个数
File file = new File(path);//需要统计行数的文件夹路径
traverseFiles(file);//调用递归方法查看.java文件,用于统计行数
System.out.println(countTxt + "代码文件个数:" + j);
System.out.println("代码总行数:" + i);
System.out.println();
}
public static void traverseFiles(File file) throws IOException {
if (!file.exists()) {//文件不存在
return;
}
if (!file.isDirectory()) {//判断是否为文件
String filename = file.getName();
if (filename.endsWith(".java")) {//判断是否是.java文件
j++;
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String string = null;
while ((string = bufferedReader.readLine()) != null) {
i++;//读取行数
}
} else
return;
}
File[] files = file.listFiles();//读取文件夹的子文件或子文件夹
if (files == null || files.length == 0) {
return;
}
for (File file2 : files) {//如果是文件夹递归调用方法遍历文件
traverseFiles(file2);
}
}
}