A common task in programming is to perform operations on sets of files, either in the local directory or by walking the entire durectory tree.
It is useful to have a tool that will produce the set of files for you.
package think.in.java.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;
/**
* Produce a seqence of File objects that match a regular expression in either a local directory,
* or by walking a directory tree.
* @author WPeng
*
* @since 2012-11-2
*/
public final class Directory {
public static File[] local(File dir, final String regex){
/** Returns an array of abstract pathnames denoting the files and directories in the directory
* denoted by this abstract pathname that satisfy the specified filter. */
return dir.listFiles(new FilenameFilter() {
private Pattern pattern = Pattern.compile(regex);
@Override
public boolean accept(File dir, String name) {
return pattern.matcher(new File(name).getName()).matches();
}
});
}
public static File[] local(String path, final String regex){
//Overloaded
return local(new File(path), regex);
}
// A two-tuple for returning a pair of objects
public static class TreeInfo implements Iterable<File>{
public List<File> files = new ArrayList<File>();
public List<File> dirs = new ArrayList<File>();
// The default iterable element is the file list
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){
// Begin recurion
return recurseDirs(new File(start), regex);
}
public static TreeInfo walk(File start, String regex){
// Overload
return recurseDirs(start, regex);
}
public static TreeInfo walk(File start){
// Everything
return recurseDirs(start, ".*");
}
public static TreeInfo walk(String start){
// Overload
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{ // Regular file
if (item.getName().matches(regex)) {
result.files.add(item);
}
}
}
return result;
}
// Simple validation test
public static void main(String[] args){
if(args.length == 0){
System.out.println(walk("D:\\Users\\wpeng\\workspace\\Hello\\src\\think\\in\\java\\io"));
}else{
for(String arg : args){
System.out.println(arg);
}
}
}
}
package think.in.java.io;
import java.util.Arrays;
import java.util.Collection;
/**
* Pretty-printer for collections
* @author WPeng
*
* @since 2012-11-2
*/
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)));
}
}
/**
dirs: [
D:\Users\wpeng\workspace\Hello\src\think\in\java\io\1
D:\Users\wpeng\workspace\Hello\src\think\in\java\io\2
D:\Users\wpeng\workspace\Hello\src\think\in\java\io\3
]
files: [
D:\Users\wpeng\workspace\Hello\src\think\in\java\io\1.txt
D:\Users\wpeng\workspace\Hello\src\think\in\java\io\2.txt
D:\Users\wpeng\workspace\Hello\src\think\in\java\io\3.txt
D:\Users\wpeng\workspace\Hello\src\think\in\java\io\Directory.java
D:\Users\wpeng\workspace\Hello\src\think\in\java\io\DirList.java
D:\Users\wpeng\workspace\Hello\src\think\in\java\io\DirList2.java
D:\Users\wpeng\workspace\Hello\src\think\in\java\io\DirList3.java
D:\Users\wpeng\workspace\Hello\src\think\in\java\io\PPrint.java
]
*/