DirectoryStream Class
Package: java.nio.file
The DirectoryStream class represents a collection of path objects contained in a directory. The DirectoryStream class implements the Iterable interface, which means that it can be used to read the contents of a directory using an enhanced for statement.
To create a DirectoryStream object, use the static newDirectoryStream method of the Files class. For more information, see Files Class.
Method
|
Method |
Description |
|
|
Returns an |
Here’s an example that retrieves the contents of a directory and prints each item on the console:
Path c = Paths.get(“C:\\myfolder”);
try
{
DirectoryStream<Path> stream
= Files.newDirectoryStream(c);
for (Path entry: stream)
System.out.println(entry.toString());
}
catch (Exception e)
{
System.out.println(“Error: “ + e.getMessage());
}
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access