對程序語言的設(shè)計者來說施流,創(chuàng)建一個好的輸出/輸入(I/O)系統(tǒng)是一項艱難的任務(wù)鄙信。——《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("."));
}
/**
* 根據(jù)正則表達(dá)式匹配文件,獲得File數(shù)組
* @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);
}
/**
* 實現(xiàn)了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);
}
}
/**
* 將目錄名字轉(zhuǎn)化為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;
/**
* "靈巧打印機(jī)"
* @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);
}
}