package com.file; import java.io.File; import java.io.FilenameFilter; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.regex.Pattern; public final class Directory { public static File[] local(File dir, final String regex) { return dir.listFiles(new FilenameFilter() { private Pattern pattern = Pattern.compile(regex); public boolean accept(File dir, String name) { return pattern.matcher(name).matches(); } }); } public static File[] local(String path, final String regex) { return local(new File(path), regex); } public static class TreeInfo implements Iterable<File> { public List<File> files = new ArrayList<File>(); public List<File> dirs = new ArrayList<File>(); public Iterator<File> iterator() { return files.iterator(); } void addAll(TreeInfo other) { files.addAll(other.files); dirs.addAll(other.dirs); } public String toString() { return "dirs:" + PPrint.pformat(dirs) + "\n\nfiles:" + PPrint.pformat(files); } } public static TreeInfo walk(String start, String regex) { return recurseDirs(new File(start), regex); } public static TreeInfo walk(String start) { return recurseDirs(new File(start), ".*"); } public static TreeInfo walk(File start, String regex) { return recurseDirs(start, regex); } public static TreeInfo walk(File start) { return recurseDirs(start, ".*"); } static TreeInfo recurseDirs(File startDir, String regex) { TreeInfo result = new TreeInfo(); for (File item : startDir.listFiles()) { if (item.isDirectory()) { result.dirs.add(item); result.addAll(recurseDirs(item, regex)); } else { if (item.getName().matches(regex)) result.files.add(item); } } return result; } /** * 调用方式 * @param args */ public static void main(String[] args) { System.out.println(local(".","D.*")); System.out.println(walk(".","D.*\\.java")); System.out.println(walk(".", "[Dd].*\\.*")); System.out.println(walk(".",".*\\.class")); } }
JAVA I/O 非常实用的目录列表器
最新推荐文章于 2021-08-07 17:26:47 发布