前言
最近为了创新学分在申请软著, 软著申请表上要求标注代码行数, 而且还需要贴源代码. 但是作为懒宅晚期患者, 我感觉数代码行数和粘贴代码超级无聊. 我就想到之前老师说一个学长用 python 写过类似的工具来统计代码行数, 然后我就想 Java 能不能实现. 经过简单的封装就用 Java 写了这个简单的工具类来统计代码. 当然你也可以使用这个工具类将源代码输出到 txt 文件里. 代码比较简单, 没有处理注释和空行, 如果想处理注释和空行也可以自己加点逻辑 (空行主要使用 StringUtils 中的 isEmpty() 方法判断, 注释根据注释习惯可以使用正则表达式处理).
代码
- import java.io.*;
- /**
- * @ClassName: CodeCount
- * @date: 2020/11/27 14:50
- * @author 索半斤
- * @Description: 小工具, 用来申请软著时自动统计代码行数并能够将代码输出到 txt 文本
- */
- public class CodeCount {
- private static int i,j = 0;
- /**
- * @MethodName: countCodeLine
- * @date: 2020/11/27 14:36
- * @author 索半斤
- * @Description: 用于统计代码行数
- * @param path 要扫描的包的绝对路径
- * @param suffix 要统计的文件后缀, 例如 java
- */
- public static Integer countCodeLine(String path,String suffix){
- return getFileOrCodeCount(path, suffix, "i");
- }
- /**
- * @MethodName: countFiles
- * @date: 2020/11/27 14:37
- * @author 索半斤
- * @Description: 用于统计文件数
- * @param path 要扫描的包的绝对路径
- * @param suffix 要统计的文件的后缀
- */
- public static Integer countFiles(String path,String suffix){
- return getFileOrCodeCount(path, suffix, "j");
- }
- /**
- * @MethodName: outFile
- * @date: 2020/11/27 14:43
- * @author 索半斤
- * @Description: 将代码输出到 txt 文件中(自动换行, 默认目录)
- * @param path 要扫描的包的绝对路径
- * @param suffix 要统计的文件的后缀
- * @param name 输出的文件名称
- */
- public static void outFile(String path,String suffix,String name){
- File file = new File(path);
- BufferedWriter bufferedWriter;
- try {
- bufferedWriter = new BufferedWriter(new FileWriter(name+".txt"));
- traverseFiles(file,bufferedWriter,suffix);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * @MethodName: outFileToPath
- * @date: 2020/11/27 14:45
- * @author 索半斤
- * @Description: 将代码写入到文本文件并输出到指定的目录
- * @param path 要扫描的包的绝对路径
- * @param suffix 要统计的文件的后缀
- * @param Filepath 要输出的文件夹绝对路径
- */
- public static void outFileToPath(String path,String suffix,String Filepath){
- boolean b = Filepath.endsWith(".txt");
- if (!b)return;
- File file = new File(path);
- BufferedWriter bufferedWriter;
- try {
- bufferedWriter = new BufferedWriter(new FileWriter(Filepath));
- traverseFiles(file,bufferedWriter,suffix);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * @MethodName: getFileOrCodeCount
- * @date: 2020/11/27 14:46
- * @author 索半斤
- * @Description: 获取代码行数或者文件数量
- */
- private static Integer getFileOrCodeCount(String path,String suffix,String type){
- File file = new File(path);
- try {
- traverseFiles(file,suffix);
- if (type.equals("i"))return i;
- else if (type.equals("j"))return j;
- else return null;
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
- }
- private static void traverseFiles(File file, BufferedWriter bufferedWriter,String suffix) throws IOException {
- if(!file.exists()){
- return;
- }
- if(!file.isDirectory()){
- String filename = file.getName();
- if(filename.endsWith("."+suffix)){
- BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
- String string =null;
- while ((string = bufferedReader.readLine()) != null) {
- //if (StringUtils.isEmpty(string))continue; 处理空行
- bufferedWriter.write(string);
- bufferedWriter.newLine();
- bufferedWriter.flush();
- }
- }else
- return;
- }
- File[] files =file.listFiles();
- if (files == null || files.length == 0) {
- return;
- }
- for(File file2 : files){
- traverseFiles(file2,bufferedWriter,suffix);
- }
- }
- private static void traverseFiles(File file,String suffix) throws IOException {
- if(!file.exists()){
- return;
- }
- if(!file.isDirectory()){
- String filename = file.getName();
- if(filename.endsWith("."+suffix)){
- 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,suffix);
- }
- }
- }
来源: http://www.bubuko.com/infodetail-3713393.html