@DefaultNonNull public class FileIterator extends Object implements Iterator<FileIterator.Entry>, Iterable<FileIterator.Entry>
File path = new File("/path/to/dir");
for(Entry entry: new FileIterator(path)) {
//-- Use information from entry like File, type, and whether this is a "before" or "after" directory call.
}
See main(String[]) for a runnable example of it's use.
The iterator traverses the entire directory tree starting (but excluding) with the path passed. It walks all files and all directories in the order that they are encountered. Files are reported once (meaning each file will return a single Entry), but directories will be reported twice: once before the files belonging inside the directory are traversed and once immediately after. This means a directory is always seen twice, and while traversing you can decide when to do something with that directory. For example when deleting a structure with this iterator you would delete files immediately but directories when the entry type is DirectoryAfter.
| Modifier and Type | Class and Description |
|---|---|
static class |
FileIterator.Entry |
static class |
FileIterator.Type |
| Constructor and Description |
|---|
FileIterator(File root) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
hasNext() |
Iterator<FileIterator.Entry> |
iterator() |
static void |
main(String[] args)
Runnable example of usage.
|
FileIterator.Entry |
next() |
void |
remove() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitforEachRemainingforEach, spliteratorpublic FileIterator(File root)
public void remove()
remove in interface Iterator<FileIterator.Entry>public boolean hasNext()
hasNext in interface Iterator<FileIterator.Entry>public FileIterator.Entry next()
next in interface Iterator<FileIterator.Entry>public Iterator<FileIterator.Entry> iterator()
iterator in interface Iterable<FileIterator.Entry>Copyright © 2017 etc.to. All rights reserved.