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 matching the String representation
of their file names against the given globbing pattern.
For example, suppose we want to iterate over the files ending with ".java" in a directory:
Path dir = ...
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.java")) {
:
}
The globbing pattern is specified by the getPathMatcher method.
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.
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.
dir
| the path to the directory | |
glob
| the glob pattern |
DirectoryStream object
java.util.regex.PatternSyntaxException
| if the pattern is invalid | |
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