All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aQute.libg.fileiterator.FileIterator Maven / Gradle / Ivy

There is a newer version: 2.0.0.20130123-133441
Show newest version
package aQute.libg.fileiterator;

import java.io.*;
import java.util.*;

public class FileIterator implements Iterator {
    File         dir;
    int          n = 0;
    FileIterator next;

    public FileIterator(File nxt) {
        assert nxt.isDirectory();
        this.dir = nxt;
    }

    public boolean hasNext() {
        if (next != null)
            return next.hasNext();
        else
            return n < dir.list().length;
    }

    public File next() {
        if (next != null) {
            File answer = next.next();
            if (!next.hasNext())
                next = null;
            return answer;
        } else {
            File nxt = dir.listFiles()[n++];
            if (nxt.isDirectory()) {
                next = new FileIterator(nxt);
                return nxt;
            } else if (nxt.isFile()) {
                return nxt;
            } else
                throw new IllegalStateException("File disappeared");
        }
    }

    public void remove() {
        throw new UnsupportedOperationException(
                "Cannot remove from a file iterator");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy