查询项目代码行数
今天领导给布置了一个任务,就是用工具查询一下项目的代码行数,但是我用工具和java工具类查询发现结果集却不一样,想了想可能是注释的过滤原因,所以手动的更改了一下java工具类,以下便是代码:
package com.example.demo;
import java.io.*;
/**
* @Author: Mr.wang
* @Date: 2019/7/12 9:50
* @Version 1.0
* @项目名称 springboot-jpa
* @包名:com.example.demo QQ:1452561187
**/
public class FileUtils {
private static int i;//代码总行数
private static int j;//文件个数
public static void main(String args[]) throws IOException {
System.out.println("file Go...");
// 这里改成你要遍历的目录路径
recursiveFiles("E:\\IdeaWorkSpace\\witsm");
System.out.println("file End.");
System.out.println("所写文件个数:" + j);
System.out.println("所写代码总行数:" + i);
}
/**
* Xiwi
* 遍历文件/文件夹 - 函数
* [String]path 文件路径
*/
private static void recursiveFiles(String path) throws IOException {
// 创建 File对象
File file = new File(path);
if (!file.exists()) {
System.out.println("文件夹不存在!!!");
return;
}
// 取 文件/文件夹
File files[] = file.listFiles();
// 对象为空 直接返回
if (files == null) {
System.out.println("文件夹为空!!!");
return;
}
// 目录下文件
if (files.length == 0) {
System.out.println(path + "该文件夹下没有文件");
}
// 存在文件 遍历 判断
for (File f : files) {
// 判断是否为 文件夹
if (f.isDirectory()) {
System.out.print("文件夹: ");
System.out.println(f.getAbsolutePath());
// 为 文件夹继续遍历
recursiveFiles(f.getAbsolutePath());
// 判断是否为 文件
} else if (f.isFile()) {
System.out.print("文件: ");
System.out.println(f.getAbsolutePath());
String filename = f.getName();
if (filename.endsWith(".java")) {//判断是否是.java文件
System.out.println("文件地址:" + file.getPath());
j++;
BufferedReader bufferedReader = new BufferedReader(new FileReader(f));
String string = null;
while ((string = bufferedReader.readLine()) != null) {
//进行过滤注释
if (string.indexOf("/*") > -1) {
continue;
} else if (string.indexOf("*") > -1) {
continue;
} else if (string.indexOf("//") > -1) {
continue;
} else {
System.out.println("string=============" + string);
i++;//读取行数
}
}
}
} else {
System.out.print("未知错误文件");
}
}
}
}