Last active
July 26, 2016 04:14
-
-
Save AlbertHG/943f27f4fe3606db2df56ed0839b98a9 to your computer and use it in GitHub Desktop.
目录实用工具
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FilenameFilter; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collection; | |
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 patt = Pattern.compile(regex);// 条件检索语句,检索regex正则表达式 | |
@Override | |
public boolean accept(File dir, String name) { | |
return patt.matcher(new File(name).getName()).matches();// 返回检索(过滤)结果 | |
} | |
}); | |
} | |
public static File[] local(String path, final String regex) { | |
return local(new File(path), regex); | |
} | |
/** | |
* 遍历并添加找到的目录到List中 | |
* @author A | |
* | |
*/ | |
public static class TreeInfo implements Iterable<File> { | |
public List<File> files = new ArrayList<File>(); | |
public List<File> dirs = new ArrayList<File>(); | |
@Override | |
public Iterator<File> iterator() { | |
return files.iterator(); | |
} | |
void addAll(TreeInfo ti) { | |
files.addAll(ti.files); | |
dirs.addAll(ti.dirs); | |
} | |
public String toString() { | |
return "dirs : "+ PPrint.pformat(dirs)+ "\n\nfles : " + PPrint.pformat(files); | |
} | |
} | |
/** | |
* 添加路径和检索正则表达式 | |
* @param start | |
* @param regex | |
* @return | |
*/ | |
public static TreeInfo walk(String start, String regex) { | |
return recuriseDirs(new File(start), regex); | |
} | |
/** | |
* 添加File实例对象和检索正则表达式 | |
* @param start | |
* @param regex | |
* @return | |
*/ | |
public static TreeInfo walk(File start, String regex) { | |
return recuriseDirs(start, regex); | |
} | |
/** | |
* 添加File实例对象 ,遍历所有文件 | |
* @param start | |
* @return | |
*/ | |
public static TreeInfo walk(File start) { | |
return recuriseDirs(start, ".*"); | |
} | |
/** | |
* 添加路径 ,遍历所有文件 | |
* @param start | |
* @return | |
*/ | |
public static TreeInfo walk(String start) { | |
return recuriseDirs(new File(start), ".*"); | |
} | |
static TreeInfo recuriseDirs(File file, String regex) { | |
TreeInfo result = new TreeInfo(); | |
for (File item : file.listFiles()) { | |
if (item.isDirectory()) { | |
result.dirs.add(item); | |
result.addAll(recuriseDirs(item, regex)); | |
} else { | |
if (item.getName().matches(regex)) { | |
result.files.add(item); | |
} | |
} | |
} | |
return result; | |
} | |
// public static void main(String[] args) { | |
// // TODO Auto-generated method stub | |
// | |
// if (args.length == 0) { | |
// System.out.println(walk(".")); | |
// } else { | |
// for (String string : args) { | |
// System.out.println(walk(string)); | |
// } | |
// } | |
// | |
// } | |
} | |
/** | |
* toString 方法使用一个“灵巧打印机”类,以使输出更容易浏览。 | |
* 容器默认的toString方法会在单个行中打印容器的所有元素,对于大型集合来说 | |
* 这意味着难以阅读 | |
* 所以现在使用一个可替换的格式化机制 | |
* 这是一个可以添加新行并缩进排列所有元素的工具 | |
* @author A | |
* | |
*/ | |
class PPrint { | |
public static String pformat(Collection<?> c) { | |
if (c.size() == 0) {//处理没有元素的情况 | |
return "[]"; | |
} | |
StringBuilder result = new StringBuilder("["); | |
for (Object elem : c) { | |
if (c.size() != 1) {//不止一个元素,每添加一个元素 换行 | |
result.append("\n "); | |
} | |
result.append(elem); | |
} | |
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 pptint(Object[] c) { | |
System.out.println(pformat(Arrays.asList(c))); | |
} | |
} | |
/** | |
* 测试工具类 | |
* @author A | |
* | |
*/ | |
class Test { | |
public static void main(String[] args) { | |
//所有的目录 | |
PPrint.pprint(Directory.walk(".").dirs); | |
//所有以”T“开头的文件 | |
for (File file : Directory.local(".", "T.*")) { | |
System.out.println(file); | |
} | |
System.out.println("-----------------------"); | |
//所有以“T”开头的Java文件 | |
for (File file : Directory.walk(".","T.*\\.java")) { | |
System.out.println(file); | |
} | |
System.out.println("======================="); | |
//所有包含Z或者z的class文件 | |
for (File file : Directory.walk(".",".*[Zz].*\\.class")) { | |
System.out.println(file); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 执行查找具有特定拓展名的文件, 如果没有制定任何参数,则假设遍历当前目录下的所有文件 | |
* @author A | |
* | |
*/ | |
public class ProcessFiles { | |
public interface Strategy { | |
void process(File file); | |
} | |
private Strategy strategy; | |
private String ext; | |
public ProcessFiles(Strategy strategy, String ext) { | |
super(); | |
this.strategy = strategy; | |
this.ext = ext; | |
} | |
public void start(String[] args){ | |
try { | |
if (args.length ==0 ) {//如果没有输入,则默认是检索当前程序所在路径下的所有文件 | |
processDirectoryTree(new File(".")); | |
}else { | |
for (String arg : args) { | |
File fileArg = new File(arg); | |
if (fileArg.isDirectory()){//测试是一个目录 | |
processDirectoryTree(fileArg); | |
}else {//如果不是目录,则测试是否为某个后缀名结束的文件 | |
if (!arg.endsWith("."+ ext)) { | |
arg += "." + ext ; | |
} | |
strategy.process(new File(arg).getCanonicalFile());//返回此抽象路径名的规范形式 | |
} | |
} | |
} | |
} catch (IOException e) { | |
throw new RuntimeException(); | |
} | |
} | |
private void processDirectoryTree(File root) throws IOException { | |
for (File file : Directory.walk(root.getAbsolutePath(), ".*\\."+ext)) { | |
strategy.process(file.getCanonicalFile()); | |
} | |
} | |
public static void main(String[] args) { | |
new ProcessFiles(new Strategy() { | |
@Override | |
public void process(File file) { | |
// TODO Auto-generated method stub | |
System.out.println(file); | |
} | |
}, "java").start(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
程序设计中一项常见的任务就是在文件集上进行操作,这些文件要么就在本地目录上,要么遍布于整个目录树上,这个工具可以为我们产生这个文件集,该工具可以通过使用local()方法产生由本地目录中的文件构成的File对象数组,或者通过walk()方法产生给定目录树下的所有文件构成的List,这些文件基于提供的正则表达式所筛选