java.nio.file
Interface DirectoryStream<T>

Type Parameters:
T - The type of element returned by the iterator
All Superinterfaces:
Closeable, Iterable<T>
All Known Implementing Classes:
SecureDirectoryStream

public interface DirectoryStream<T>
extends Closeable, Iterable<T>

Disabled: no SafeJ information.

An object to iterate over the entries in a directory. A directory stream allows for convenient use of the for-each construct:

   Path dir = ...
   DirectoryStream<Path> stream = dir.newDirectoryStream();
   try {
       for (Path entry: stream) {
         ..
       }
   } finally {
       stream.close();
   }
 

A DirectoryStream is not a general-purpose Iterable. While this interface extends Iterable, the iterator method may only be invoked once to obtain the iterator; a second, or subsequent, call to the iterator method throws IllegalStateException.

A DirectoryStream is opened upon creation and is closed by invoking the close method. Closing the directory stream releases any resources associated with the stream. Once a directory stream is closed, all further method invocations on the iterator throw ConcurrentModificationException with cause ClosedDirectoryStreamException.

A directory stream is not required to be asynchronously closeable. If a thread is blocked on the directory stream's iterator reading from the directory, and another thread invokes the close method, then the second thread may block until the read operation is complete.

The hasNext and next methods can encounter an I/O error when iterating over the directory in which case ConcurrentModificationException is thrown with cause IOException. The hasNext method is guaranteed to read-ahead by at least one element. This means that if the hasNext method returns true and is followed by a call to the next method then it is guaranteed not to fail with a ConcurrentModificationException.

The elements returned by the iterator are in no specific order. Some file systems maintain special links to the directory itself and the directory's parent directory. Entries representing these links are not returned by the iterator.

The iterator's remove method removes the directory entry for the last element returned by the iterator, as if by invoking the delete method. If an I/O error or security exception occurs then ConcurrentModificationException is thrown with the cause.

The iterator is weakly consistent. It is thread safe but does not freeze the directory while iterating, so it may (or may not) reflect updates to the directory that occur after the DirectoryStream is created.

Since:
1.7
See Also:
Path.newDirectoryStream()

Nested Class Summary
static interface DirectoryStream.Filter<T>
          An interface that is implemented by objects that decide if a directory entry should be accepted or filtered.
 
Method Summary
 Iterator<T> iterator()
          Returns the iterator associated with this DirectoryStream.
 
Methods inherited from interface java.io.Closeable
close
 

Method Detail

iterator

Iterator<T> iterator()
Class is disabled.

Returns the iterator associated with this DirectoryStream.

Specified by:
iterator in interface Iterable<T>
Returns:
the iterator associated with this DirectoryStream
Throws:
IllegalStateException - if this directory stream is closed or the iterator has already been returned