-
- Type Parameters:
T
- The type of element returned by the iterator
- All Superinterfaces:
- AutoCloseable, Closeable, Iterable<T>
- All Known Subinterfaces:
- SecureDirectoryStream<T>
public interface DirectoryStream<T> extends Closeable, Iterable<T>
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.While
DirectoryStream
extendsIterable
, it is not a general-purposeIterable
as it supports only a singleIterator
; invoking theiterator
method to obtain a second or subsequent iterator throwsIllegalStateException
.An important property of the directory stream's
Iterator
is that itshasNext
method is guaranteed to read-ahead by at least one element. IfhasNext
method returnstrue
, and is followed by a call to thenext
method, it is guaranteed that thenext
method will not throw an exception due to an I/O error, or because the stream has beenclosed
. TheIterator
does not support theremove
operation.A
DirectoryStream
is opened upon creation and is closed by invoking theclose
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<Path> 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, theIterator
may return one or more elements after the directory stream has been closed. Once these buffered elements have been read, then subsequent calls to thehasNext
method returnsfalse
, and subsequent calls to thenext
method will throwNoSuchElementException
.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
'shasNext
ornext
methods to throwDirectoryIteratorException
with theIOException
as the cause. As stated above, thehasNext
method is guaranteed to read-ahead by at least one element. This means that ifhasNext
method returnstrue
, and is followed by a call to thenext
method, then it is guaranteed that thenext
method will not fail with aDirectoryIteratorException
.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.Usage Examples: 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<Path> listSourceFiles(Path dir) throws IOException { List<Path> result = new ArrayList<>(); try (DirectoryStream<Path> 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; }
- Since:
- 1.7
- See Also:
Files.newDirectoryStream(Path)
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface and Description static interface
DirectoryStream.Filter<T>
An interface that is implemented by objects that decide if a directory entry should be accepted or filtered.
-
-
-
Method Detail
-
iterator
Iterator<T> iterator()
Returns the iterator associated with thisDirectoryStream
.- Specified by:
iterator
in interfaceIterable<T>
- Returns:
- the iterator associated with this
DirectoryStream
- Throws:
IllegalStateException
- if this directory stream is closed or the iterator has already been returned
-
-
Traduction non disponible
Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Version en cache
22/11/2024 04:41:50 Cette version de la page est en cache (à la date du 22/11/2024 04:41:50) afin d'accélérer le traitement. Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la dernère version de la page.Document créé le 11/06/2005, dernière modification le 04/03/2020
Source du document imprimé : https://www.gaudry.be/java-api-rf-java/nio/file/directorystream.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.