hj_代码行数统计

闲来无事,统计下代码行数,空格行数,字符个数等~
package com.horuse;

import java.io.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hj
 * @Date 2022/7/20
 * @Description:
 */
public class HjTest {


    private static int directoryNum = 0;
    private static int javaFileNum = 0;
    private static int javaCodeRows = 0;
    private static int javaLetterNum = 0;
    private static int javaSpaceRows = 0;
    private static int xmlFileNum = 0;
    private static int ymlFileNum = 0;
    private static int rarNum = 0;
    private static int totalCodeRows = 0;
    private static int totalSpaceRows = 0;

    public static void main(String[] args) throws IOException {
        String filePath1 = "D:\\WORK\\horus\\idea\\upleaf";
        String filePath2 = "D:\\WORK\\horus\\idea\\hauscal";
        File dir1 = new File(filePath1);
        statistics(dir1);
        File dir2 = new File(filePath2);
        statistics(dir2);
        System.out.println("统计的根目录是:\n" + filePath1+"\n"+filePath2);
        System.out.printf("目录个数为:%s \n", directoryNum);
        System.out.printf("java文件个数为:%s \n", javaFileNum);
        System.out.printf("java代码行数为:%s \n", javaCodeRows);
        System.out.printf("java空格行数为:%s \n", javaSpaceRows);
        System.out.printf("java字符个数:%s \n", javaLetterNum);
        System.out.printf("xml文件个数为:%s \n", xmlFileNum);
        System.out.printf("yml文件个数为:%s \n", ymlFileNum);
        System.out.printf("rar文件个数为:%s \n", rarNum);
        System.out.printf("总行数为:%s \n", totalCodeRows);
        System.out.printf("总空格行数为:%s \n", totalSpaceRows);
    }


    private static void statistics(File dir) throws IOException {
        // 目录
        if (dir.isDirectory()) {
            directoryNum++;
            File[] listFile = dir.listFiles();
            if (null == listFile) {
                return;
            }
            for (File f : listFile) {
                if (f.isDirectory()) {
                    // 递归调用
                    statistics(f);
                }
                BufferedReader br;
                if (f.isFile()) {
                    String fName = f.getName();
                    // 文件后缀
                    if (fName.contains(".")) {
                        String substring = fName.substring(fName.lastIndexOf(".") + 1);
                        switch (substring) {
                            case "java":
                                javaFileNum++;
                                br = new BufferedReader(new FileReader(f));
                                javaCodeRows += br.lines().filter(line -> line.trim().length() > 0).count();
                                br = new BufferedReader(new FileReader(f));
                                javaSpaceRows += br.lines().filter(line -> line.trim().length() == 0).count();
                                br = new BufferedReader(new FileReader(f));
                                List<String> collect = br.lines().collect(Collectors.toList());
                                collect.forEach(s -> javaLetterNum += s.replace(" ", "").length());
                                break;
                            case "xml":
                                xmlFileNum++;
                                break;
                            case "yml":
                                ymlFileNum++;
                                break;
                            case "rar":
                                rarNum++;
                                break;
                            default:
                                continue;
                        }
                    }
                    br = new BufferedReader(new FileReader(f));
                    totalCodeRows += br.lines().filter(line -> line.trim().length() > 0).count();
                    br = new BufferedReader(new FileReader(f));
                    totalSpaceRows += br.lines().filter(line -> line.trim().length() == 0).count();
                }
            }
        }

    }


}

  

posted @ 2022-07-20 16:15  独孤~华剑  阅读(36)  评论(0编辑  收藏  举报
独孤华剑