Compartir a través de


IDirectoryStream Interface

Definition

An object to iterate over the entries in a directory.

[Android.Runtime.Register("java/nio/file/DirectoryStream", "", "Java.Nio.FileNio.IDirectoryStreamInvoker", ApiSince=26)]
[Java.Interop.JavaTypeParameters(new System.String[] { "T" })]
public interface IDirectoryStream : IDisposable, Java.Interop.IJavaPeerable, Java.IO.ICloseable, Java.Lang.IIterable
[<Android.Runtime.Register("java/nio/file/DirectoryStream", "", "Java.Nio.FileNio.IDirectoryStreamInvoker", ApiSince=26)>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "T" })>]
type IDirectoryStream = interface
    interface ICloseable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
    interface IIterable
Derived
Attributes
Implements

Remarks

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

<b> While DirectoryStream extends Iterable, it is not a general-purpose Iterable as it supports only a single Iterator; invoking the #iterator iterator method to obtain a second or subsequent iterator throws IllegalStateException. </b>

An important property of the directory stream's Iterator is that its Iterator#hasNext() hasNext method is guaranteed to read-ahead by at least one element. If hasNext method returns true, and is followed by a call to the next method, it is guaranteed that the next method will not throw an exception due to an I/O error, or because the stream has been #close closed. The Iterator does not support the Iterator#remove remove operation.

A DirectoryStream is opened upon creation and is closed by invoking the close method. Closing a directory stream releases any resources associated with the stream. Failure to close the stream may result in a resource leak. The try-with-resources statement provides a useful construct to ensure that the stream is closed:

Path dir = ...
              try (DirectoryStream&lt;Path&gt; stream = Files.newDirectoryStream(dir)) {
                  for (Path entry: stream) {
                      ...
                  }
              }

Once a directory stream is closed, then further access to the directory, using the Iterator, behaves as if the end of stream has been reached. Due to read-ahead, the Iterator may return one or more elements after the directory stream has been closed. Once these buffered elements have been read, then subsequent calls to the hasNext method returns false, and subsequent calls to the next method will throw NoSuchElementException.

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.

If an I/O error is encountered when accessing the directory then it causes the Iterator's hasNext or next methods to throw DirectoryIteratorException with the IOException as the cause. As stated above, the hasNext method is guaranteed to read-ahead by at least one element. This means that if hasNext method returns true, and is followed by a call to the next method, then it is guaranteed that the next method will not fail with a DirectoryIteratorException.

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 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.

<b>Usage Examples:</b> Suppose we want a list of the source files in a directory. This example uses both the for-each and try-with-resources constructs.

List&lt;Path&gt; listSourceFiles(Path dir) throws IOException {
                  List&lt;Path&gt; result = new ArrayList&lt;&gt;();
                  try (DirectoryStream&lt;Path&gt; stream = Files.newDirectoryStream(dir, "*.{c,h,cpp,hpp,java}")) {
                      for (Path entry: stream) {
                          result.add(entry);
                      }
                  } catch (DirectoryIteratorException ex) {
                      // I/O error encounted during the iteration, the cause is an IOException
                      throw ex.getCause();
                  }
                  return result;
              }

Added in 1.7.

Java documentation for java.nio.file.DirectoryStream.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Properties

Handle

Gets the JNI value of the underlying Android object.

(Inherited from IJavaObject)
JniIdentityHashCode

Returns the value of java.lang.System.identityHashCode() for the wrapped instance.

(Inherited from IJavaPeerable)
JniManagedPeerState

State of the managed peer.

(Inherited from IJavaPeerable)
JniPeerMembers

Member access and invocation support.

(Inherited from IJavaPeerable)
PeerReference

Returns a JniObjectReference of the wrapped Java object instance.

(Inherited from IJavaPeerable)

Methods

Close()

Closes this stream and releases any system resources associated with it.

(Inherited from ICloseable)
Disposed()

Called when the instance has been disposed.

(Inherited from IJavaPeerable)
DisposeUnlessReferenced()

If there are no outstanding references to this instance, then calls Dispose(); otherwise, does nothing.

(Inherited from IJavaPeerable)
Finalized()

Called when the instance has been finalized.

(Inherited from IJavaPeerable)
ForEach(IConsumer)

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

(Inherited from IIterable)
Iterator()

Returns the iterator associated with this DirectoryStream.

SetJniIdentityHashCode(Int32)

Set the value returned by JniIdentityHashCode.

(Inherited from IJavaPeerable)
SetJniManagedPeerState(JniManagedPeerStates) (Inherited from IJavaPeerable)
SetPeerReference(JniObjectReference)

Set the value returned by PeerReference.

(Inherited from IJavaPeerable)
Spliterator()

Creates a Spliterator over the elements described by this Iterable.

(Inherited from IIterable)
UnregisterFromRuntime()

Unregister this instance so that the runtime will not return it from future Java.Interop.JniRuntime+JniValueManager.PeekValue invocations.

(Inherited from IJavaPeerable)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)
ToEnumerable(IIterable)
ToEnumerable<T>(IIterable)

Applies to