Opens a directory, returning a DirectoryStream
to iterate over
the entries in the directory. The elements returned by the directory
stream's iterator
are of type
Path
, each one representing an entry in the directory. The Path
objects are obtained as if by resolving
the
name of the directory entry against dir
. The entries returned by
the iterator are filtered by the given filter
.
When not using the try-with-resources construct, then directory
stream's close
method should be invoked after iteration is
completed so as to free any resources held for the open directory.
Where the filter terminates due to an uncaught error or runtime
exception then it is propagated to the Iterator.hasNext()
or next
method. Where an
IOException
is thrown, it results in the hasNext
or
next
method throwing a DirectoryIteratorException
with the
IOException
as the cause.
When an implementation supports operations on entries in the
directory that execute in a race-free manner then the returned directory
stream is a SecureDirectoryStream
.
Usage Example: Suppose we want to iterate over the files in a directory that are larger than 8K.
DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() { public boolean accept(Path file) throws IOException { return (Files.size(file) > 8192L); } }; Path dir = ... try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, filter)) { : }
dir
| the path to the directory | |
filter
| the directory stream filter |
DirectoryStream
object
NotDirectoryException
| if the file could not otherwise be opened because it is not a directory (optional specific exception) | |
IOException
| if an I/O error occurs | |
SecurityException
| In the case of the default provider, and a security manager is
installed, the checkRead
method is invoked to check read access to the directory. |
Diagram: Files