java 代码统计工具
项目上线要统计一下所有代码的行数,公司给了个工具,结果用着用着就卡掉卡死,索性自己写了一个,项目不是很大,没用到多线程等技术。下面直接贴代码:
import java.io.*;
import java.util.Scanner;public class CountRows {private static int allRowNumber;public static void main(String[] args) throws IOException {System.out.println("PS:自动省略node_modules文件");Scanner sc = new Scanner(System.in);System.out.println("请输入文件目录");String path = sc.next();File file = new File(path);if (!file.exists()) {System.out.println("目录不存在");} else {System.out.println("请输入后缀名个数");int nameCount = sc.nextInt();String[] nameArrs = new String[nameCount];for (int i = 0; i < nameCount; i++) {System.out.println("请输入第" + (i + 1) + "个文件后缀名");nameArrs[i] = sc.next();}readFile(file, nameCount, nameArrs);System.out.println("所有文件一共:" + allRowNumber + "行");}
}public static void readFile(File file, int nameCount, String[] nameArrs) throws IOException {for (File files : file.listFiles()) {if (files.isDirectory() && !"node_modules".equals(files.getName())) {readFile(files, nameCount, nameArrs);}for (int i = 0; i < nameCount; i++) {if (files.getName().endsWith(nameArrs[i])) {BufferedReader br = new BufferedReader(new FileReader(files));int nowRows = 0;while (br.ready()) {if (!"".equals(br.readLine().trim())) {nowRows++;allRowNumber++;}}System.out.println("文件名: " + files.getName() + " 行数:" + nowRows);br.close();}}}}
}
随便找一个开源项目测试一下
(PS:后缀名数量等自己定义即可)