java根据目录查找最大行数的文件,而且可过滤目录

有时候,看项目的时候,想知道哪个该项目中哪个文件是最大的,就可以采用这个程序

(还加了查找最小行数文件功能)

大部分的时候,一个项目中,如果某个文件的行数最多,一般说明该文件为此项目的核心文件,也就是核心功能或者业务代码写在这个文件中。

代码如下:

public class FindFile {
    private static final String FILEPATH = "F:\\spring";
    //需要查找的后缀
    private static final String[] NEEDFILE = {".sh", ".java", ".xml", ".yml", ".properties", ".html", ".css", ".js", ".py", ".lua"};
    //需要过滤的目录,支持*号
    private static final String[] EXCLUDEDIR = {".*", "target", "classes"};

    private FindFile() {
    }

    public static List<Map<File, Integer>> findMaxLineFile(String path) throws IOException {
        File sPath = new File(path);
        if (!sPath.isDirectory()) {
            System.err.printf("不是路径!");
            return null;
        }
        List<File> files = allFile(sPath);
        List<Map<File, Integer>> result = new ArrayList<>();
        Map<File, Integer> maxFile = new HashMap<>();
        Map<File, Integer> minFile = new HashMap<>();
        result.add(maxFile);
        result.add(minFile);
        FileReader fr = null;
        LineNumberReader lnr = null;
        int max = 0, min = Integer.MAX_VALUE;
        for (File temp : files) {
            fr = new FileReader(temp);
            lnr = new LineNumberReader(fr);
            lnr.skip(temp.length());
            int lineNumber = lnr.getLineNumber() + 1;
            if (lineNumber > max) {
                max = lineNumber;
                maxFile.clear();
                maxFile.put(temp, lineNumber);
            } else if (lineNumber == max) {
                maxFile.put(temp, lineNumber);
            }
            if (lineNumber < min) {
                min = lineNumber;
                minFile.clear();
                minFile.put(temp, lineNumber);
            } else if (lineNumber == min) {
                minFile.put(temp, lineNumber);
            }
            lnr.close();
            fr.close();
        }
        return result;
    }

    private static List<File> allFile(File file) {
        long start = System.currentTimeMillis();
        long result = 0;
        LinkedList<File> directory = new LinkedList<>();
        LinkedList<File> files = new LinkedList<>();
        if (file.isDirectory()) {
            if (!excludeDirAdapter(file.getName())) {
                return files;
            }
            directory.add(file);
        } else if (isContain(file.getName())) {
            files.add(file);
            result++;
        }
        while (!directory.isEmpty()) {
            File remove = directory.remove();
            for (File temp : remove.listFiles()) {
                if (temp.isDirectory()) {
                    if (!excludeDirAdapter(temp.getName())) {
                        continue;
                    }
                    directory.add(temp);
                } else if (isContain(temp.getName())) {
                    files.add(temp);
                    result++;
                }
            }
        }
        System.out.println("遍历文件共耗时:" + (System.currentTimeMillis() - start + "ms"));
        System.out.println("文件总共有:" + result + "个");
        return files;
    }

    private static boolean isContain(String target) {
        int index = target.lastIndexOf(".");
        if (index == -1) {
            return false;
        }
        String sub = target.substring(index);
        for (String str : NEEDFILE) {
            if (str.equals(sub)) {
                return true;
            }
        }
        return false;
    }

    private static boolean excludeDirAdapter(String target) {
        //final String notChar = "\\/:*?\"<>|";
        for (String str : EXCLUDEDIR) {
            if (str.contains("*")) {
                str = str.replace(".", "\\.").replace("*", ".*")
                        .replace("?", ".");
                if (Pattern.matches(str, target)) {
                    return false;
                }
            } else if (str.equals(target)) {
                return false;
            }

        }
        return true;
    }


    public static void main(String[] args) throws IOException {
        System.out.println(findMaxLineFile(FILEPATH));
    }

 

如果发现bug,欢迎在文章下面留言

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章