Java IO Tutorial - Java Files.newDirectoryStream(Path dir)








Syntax

Files.newDirectoryStream(Path dir) has the following syntax.

public static DirectoryStream < Path > newDirectoryStream(Path dir)      throws IOException

Example

In the following code shows how to use Files.newDirectoryStream(Path dir) method.

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.concurrent.TimeUnit;
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
//  w  ww . j av a2  s .com
public class Main {

    public static void main(String[] args) {

        Path path = Paths.get("C:/tutorial/Java/JavaFX");

        //no filter applyied
        try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
            for (Path file : ds) {
                System.out.println(file.getFileName());
            }
        } catch (IOException e) {
            System.err.println(e);
        }

    }
}