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

Iterator iterator()

Returns an Iterator object, which can be used to iterate the paths in the directory stream.

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());

}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset