Class FileTreeIterator

  extended by
All Implemented Interfaces:

public class FileTreeIterator
extends Object
implements Iterator

This class implements an iterator for a directory tree of files and subdirectories. Within each subdirectory, it returns the immediate children (files and subdirectories) in case-insensitive alphabetic order. However, after returning each subdirectory, it returns the contents of the subdirectory, recursively, before continuing with the remaining siblings of that subdirectory. That is, it performs a "depth-first pre-order left-to-right" traversal. It does not return the top-level directory that was originally specified for traversal -- only its descendants.

   - The typical scenario for using this class is:
       for (Iterator i = new FileTreeIterator("/dir"); i.hasNext(); )
           File file = (File);

   - See the source code of the inner Tester class for more examples.
       - None.  Iterates over the directory tree, but does not change it.
Anticipated Changes:
       - Currently iterates forward only.  May add hasPrev() and prev() to 
         support reverse iteration and reversing during an iteration. 
       - May add support for resetting the iteration to a specified index 
         within the current subdirectory (the 3rd file, 5th file, etc.)
       - May add support for querying the index within the current 
       - Currently caches the entire set of filenames in a single directory,
         internally as an array of Strings.  May add an option to cache less
         and retrieve more directly from the filesystem on each iteration.
       - May add new constructor with a FilenameFilter parameter, so the 
         iterator returns a set of Files filtered by name.
       - May add new constructor with a FileFilter parameter, so the 
         iterator returns a set of Files filtered by other File properties.
         Note:  Supporting FileFilter will be easiest to do by switching 
                internally from File.list() to File.listFiles(), but that
                will make it harder to sort by filename.
       - May add additional traversal orders:
         - breadth-first, instead of depth-first
         - case-sensitive, instead of case-insensitive
         - post-order, instead of pre-order
         - right-to-left, instead of left-to-right     
       - May add a limit on the depth to traverse, so caller can limit a
         traversal to children only, children and grandchildren only, etc. 
Implementation Notes:
Portability Issues:
Revision History:

Nested Class Summary
static class FileTreeIterator.Tester
          Each class contains a Tester inner class with a main() for easier unit testing.
Constructor Summary
FileTreeIterator(String strDirectoryName)
Method Summary
 boolean hasNext()
          Returns true if there are more Files to be iterated; false otherwise.
 Object next()
          Returns the next File in the iteration.
 void remove()
          Not implemented.
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail


public FileTreeIterator(String strDirectoryName)

strDirectoryName - String name of the directory tree to iterate.
NullPointerException - If strDirectoryName is null.
Method Detail


public boolean hasNext()
Returns true if there are more Files to be iterated; false otherwise.

Specified by:
hasNext in interface Iterator
true if there are more Files to be iterated; false otherwise


public Object next()
            throws NoSuchElementException
Returns the next File in the iteration.

Specified by:
next in interface Iterator
the next File in the iteration
NoSuchElementException - when no more Files in the iteration


public void remove()
Not implemented. This is an optional method of the Iterator interface.

Specified by:
remove in interface Iterator