对程序语言的设计者来说,创建一个好的输出/输入(I/O)系统是一项艰难的任务。——《Thinking in Java》
18.1 File类
18.1.1 目录列表器
Directory.java
package com.ntech.io;
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;
/**
* 目录实用工具类
* @author joiant
*
*/
public final class Directory {
public static void main(String[] args) {
System.out.println(walk("."));
}
/**
* 根据正则表达式匹配文件,获得File数组
* @param dir
* @param regex
* @return
*/
public static File[] local(File dir, final String regex) {
return dir.listFiles(new FilenameFilter() {
private Pattern pattern = Pattern.compile(regex);
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return pattern.matcher(new File(name).getName()).matches();
}
});
}
/**
* Overloaded
* @param path
* @param regex
* @return
*/
public static File[] local(String path, final String regex) {
return local(new File(path), regex);
}
/**
* 实现了Iterable<File>
* @author joiant
*
*/
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() {
// TODO Auto-generated method stub
return files.iterator();
}
void addAll(TreeInfo other) {
files.addAll(other.files);
dirs.addAll(other.dirs);
}
public String toString() {
return "dirs: " + PPrint.pformat(dirs) + "\n\n"
+ "files: " + PPrint.pformat(files);
}
}
/**
* 将目录名字转化为File对象
* @param start
* @param regex
* @return
*/
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 recurseDirs(new File(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;
}
}
PPrint.java
package com.ntech.io;
import java.util.Arrays;
import java.util.Collection;
/**
* "灵巧打印机"
* @author joiant
*
*/
public 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 pprint(Object[] c) {
System.out.println(pformat(Arrays.asList(c)));
}
}
ProcessFiles.java
package com.ntech.io;
import java.io.File;
import java.io.IOException;
public class ProcessFiles {
public interface Strategy{
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) throws IOException {
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());
}
}
}
}
public void processDirectoryTree(File root) throws IOException {
for(File file:Directory.walk(root.getAbsolutePath(), ".*\\." + ext)) {
strategy.process(file.getCanonicalFile());
}
}
public static void main(String[] args) throws IOException {
new ProcessFiles(new Strategy() {
@Override
public void process(File file) {
System.out.println(file);
}
}, "java").start(args);
}
}
网友评论