Java Get All Entries in Directory using Files.newDirectoryStream()

Tags: DirectoryStream Files.newDirectoryStream Java NIO

This Java tutorial to show you how open a directory and get all files and directories inside it using the Files.newDirectoryStream() method.

Table of contents

  1. How to use Files.newDirectoryStream()
  2. More Files.newDirectoryStream() Java Examples

How to use Files.newDirectoryStream()

For example, we have the directory D:\SimpleSolution\Data which has files and folders as following screenshot.

Java Get All Entries in Directory using Files.newDirectoryStream()

In the following Java program, we use Files.newDirectoryStream() method to read the directory and print out all files and folder inside it.

FilesNewDirectoryStreamExample1.java

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FilesNewDirectoryStreamExample1 {
    public static void main(String... args) throws IOException {
        Path path = Paths.get("D:\\SimpleSolution\\Data");
        try(DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path)) {
            for(Path file : directoryStream) {
                System.out.println(file);
            }
        }
    }
}
The output as below.
D:\SimpleSolution\Data\Document.txt
D:\SimpleSolution\Data\Folder1
D:\SimpleSolution\Data\Folder2
D:\SimpleSolution\Data\image.png

More Files.newDirectoryStream() Java Examples

Files.newDirectoryStream() Java Code Example 1

The following Java program to read content of temporary directory using the Files.newDirectoryStream() method.

FilesNewDirectoryStreamExample2.java

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FilesNewDirectoryStreamExample2 {
    public static void main(String... args) throws IOException {
        String temporaryDirectory = System.getProperty("java.io.tmpdir");
        Path path = Paths.get(temporaryDirectory);
        try(DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path)) {
            directoryStream.forEach(System.out::println);
        }
    }
}
The output as below.
C:\Users\SimpleSolution\AppData\Local\Temp\FFFRHUFSH.tmp
C:\Users\SimpleSolution\AppData\Local\Temp\+~JF3420375254451124327197.tmp
C:\Users\SimpleSolution\AppData\Local\Temp\+~JF84780551475545586547786.tmp
...

Files.newDirectoryStream() Java Code Example 2

The following Java example program to read directory and return files or folder with a specified name prefix.

FilesNewDirectoryStreamExample3.java

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FilesNewDirectoryStreamExample3 {
    public static void main(String... args) throws IOException {
        Path path = Paths.get("D:\\SimpleSolution\\Data");
        try(DirectoryStream<Path> directoryStream
                    = Files.newDirectoryStream(path, p -> p.getFileName().toString().startsWith("image"))) {
            for(Path file : directoryStream) {
                System.out.println(file);
            }
        }
    }
}
The output as below.
D:\SimpleSolution\Data\image.png

Files.newDirectoryStream() Java Code Example 3

In the following Java program, we use Files.newDirectoryStream() with filter to get directories only.

DirectoryStream<Path> stream = Files.newDirectoryStream(path, p -> Files.isDirectory(p))

FilesNewDirectoryStreamExample4.java

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FilesNewDirectoryStreamExample4 {
    public static void main(String... args) throws IOException {
        Path path = Paths.get("D:\\SimpleSolution\\Data");
        try(DirectoryStream<Path> stream = Files.newDirectoryStream(path, p -> Files.isDirectory(p))) {
            for(Path directory : stream) {
                System.out.println(directory);
            }
        }
    }
}
The output as below.
D:\SimpleSolution\Data\Folder1
D:\SimpleSolution\Data\Folder2

Files.newDirectoryStream() Java Code Example 4

In the following Java program, we use Files.newDirectoryStream() with filter to get files only.

DirectoryStream<Path> stream = Files.newDirectoryStream(path, p -> !Files.isDirectory(p))

FilesNewDirectoryStreamExample5.java

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FilesNewDirectoryStreamExample5 {
    public static void main(String... args) throws IOException {
        Path path = Paths.get("D:\\SimpleSolution\\Data");
        try(DirectoryStream<Path> stream = Files.newDirectoryStream(path, p -> !Files.isDirectory(p))) {
            for(Path file : stream) {
                System.out.println(file);
            }
        }
    }
}
The output as below.
D:\SimpleSolution\Data\Document.txt
D:\SimpleSolution\Data\image.png

Files.newDirectoryStream() Java Code Example 5

The following Java program to get all file in a directory and print out file names.

FilesNewDirectoryStreamExample6.java

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FilesNewDirectoryStreamExample6 {
    public static void main(String... args) throws IOException {
        Path path = Paths.get("D:\\SimpleSolution\\Data");
        try(DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
            for(Path file: stream) {
                if(!Files.isDirectory(file)) {
                    System.out.println(file.getFileName().toString());
                }

            }
        }
    }
}
The output as below.
Document.txt
image.png

Happy Coding 😊

Java Copy File using Files.copy()

Java Create Parent and Sub Directories using Files.createDirectories()

Java Create a New Directory using Files.createDirectory()

Java Create New File using Files.createFile()