以下代碼可能有用.
import
import java.util.concurrent.BlockingQueue;
public class FileEnumerationTask implements Runnable {
public static File DUMMY = new File("");
private BlockingQueue<File> queue;
private File staringDirectory;
public FileEnumerationTask(BlockingQueue<File> queue, File statingDirectory){
this.queue = queue;
this.staringDirectory=statingDirectory;
}
public void run(){
try{
enumerate(staringDirectory);
queue.put(DUMMY);
}
catch(InterruptedException e){
}
}
public void enumerate(File directory) throws InterruptedException{
File[] files = directory.listFiles();
for(File file:files){
if(file.isDirectory()) enumerate(file);
else queue.put(file);
}
}
}