public static void iterateFile(File file) {
if (file.isDirectory()) {
if (file.getName().startsWith(".")) return;
for (File item : file.listFiles()) {
iterateFile(item);
}
return;
}
// do something
}
if (file.isDirectory()) {
if (file.getName().startsWith(".")) return;
for (File item : file.listFiles()) {
iterateFile(item);
}
return;
}
// do something
}
但是递归函数的缺点就是扩展不方便,当然你对这个函数加入一个参数FileHandler,这样扩展性稍好一些,但是仍然不够好,比如说,不能根据遍历的需要中途停止遍历,加入Filter等等。我实现了一个FileIterator,使得遍历一个目录下的文件如何遍历一个集合中的元素一般操作。
废话少说,代码如下:
package net.wenshao;
import java.io.File;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class FileIterator implements Iterator<File> {
private static class State {
final State parent;
final File[] files;
int index = 0;
public State(State parent, File dir) {
this.parent = parent;
files = dir.listFiles();
}
}
private File current;
private State state;
public FileIterator(File file) {
if (file.isDirectory()) {
state = new State(null, file);
nextInternal();
} else {
this.current = file;
state = null;
}
}
@Override
public boolean hasNext() {
return current != null;
}
@Override
public File next() {
File rtValue = current;
if (rtValue == null) throw new NoSuchElementException();
nextInternal();
return rtValue;
}
private void nextInternal() {
current = null;
if (this.state == null) return;
for (;;) {
if (state.index >= state.files.length) {
state = state.parent;
if (state == null) return;
state.index++;
continue;
}
File file = state.files[state.index];
// 可以在此处加入Filters处理代码
if (file.isDirectory()) {
state = new State(state, file);
continue;
}
current = file;
state.index++;
break;
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
import java.io.File;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class FileIterator implements Iterator<File> {
private static class State {
final State parent;
final File[] files;
int index = 0;
public State(State parent, File dir) {
this.parent = parent;
files = dir.listFiles();
}
}
private File current;
private State state;
public FileIterator(File file) {
if (file.isDirectory()) {
state = new State(null, file);
nextInternal();
} else {
this.current = file;
state = null;
}
}
@Override
public boolean hasNext() {
return current != null;
}
@Override
public File next() {
File rtValue = current;
if (rtValue == null) throw new NoSuchElementException();
nextInternal();
return rtValue;
}
private void nextInternal() {
current = null;
if (this.state == null) return;
for (;;) {
if (state.index >= state.files.length) {
state = state.parent;
if (state == null) return;
state.index++;
continue;
}
File file = state.files[state.index];
// 可以在此处加入Filters处理代码
if (file.isDirectory()) {
state = new State(state, file);
continue;
}
current = file;
state.index++;
break;
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
使用FileIterator的例子:
File dir = new File("/home/wenshao/workspace");
Iterator<File> iter = new FileIterator(dir);
while (iter.hasNext()) {
File file = iter.next();
System.out.println(file.getPath());
}
Iterator<File> iter = new FileIterator(dir);
while (iter.hasNext()) {
File file = iter.next();
System.out.println(file.getPath());
}