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 DirUtil { //produce a sequence of File objects that match a regular expression in a local 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(new File(name).getName()).matches(); } }); } //overload local(File,String) 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(); } public 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(File start, String regex){ return recurseDirs(start, regex); } public static TreeInfo walk(String start){ return walk(start,".*"); } public static TreeInfo walk(File start){ return walk(start,".*"); } //dir recursion 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; } public static void main(String[] args) { //System.out.println(walk(".",".*l.java")); PPrint.pprint(local(".",".*\\.*")); //PPrint.pprint(walk(".").dirs); //PPrint.pprint(walk(".").files); } } import java.util.Arrays; import java.util.Collection; public class PPrint { public static String pformat(Collection<?> c){ if(c.size()==0){ return "[]"; } StringBuilder result = new StringBuilder("["); for(Object item : c){ if(c.size()!=1){ result.append("\n "); } result.append(item); } if(c.size()!=1){ result.append("\n"); } result.append("]"); return result.toString(); } public static void pprint(Collection<?> c){ System.out.println(pformat(c)); } public static void pprint(Object[] os){ System.out.println(pformat(Arrays.asList(os))); } } import java.io.File; import java.io.IOException; /** * 遍历目录,根据策略处理目录中的文件 * */ public class ProcessFiles { public interface Strategy{ public void process(File file); } private Strategy strategy; private String ext; public ProcessFiles(Strategy strategy, String ext){ this.strategy = strategy; this.ext = ext; } public void start(String[] args){ try { if(args.length==0){ processDirTree(new File(".")); }else{ for(String arg : args){ File fileArg = new File(arg); if(fileArg.isDirectory()){ processDirTree(fileArg); }else{ if(!arg.endsWith("." + ext)){ arg += "." + ext; } strategy.process(new File(arg).getCanonicalFile()); } } } } catch (IOException e) { throw new RuntimeException(e); } } public void processDirTree(File file) throws IOException { for(File child : DirUtil.walk(file.getAbsolutePath(), ".*\\." + ext)){ strategy.process(child.getCanonicalFile()); } } public static void main(String[] args) { new ProcessFiles(new ProcessFiles.Strategy(){ public void process(File file) { System.out.println(file); } },"java").start(new String[]{"."}); } }