Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this quick tutorial, we’re going to get familiar with a few ways to find out if a directory is empty or not.

2. Using Files.newDirectoryStream

As of Java 7, the Files.newDirectoryStream method returns a DirectoryStream<Path> to iterate over all the entries in the directory. So we can use this API to check if the given directory is empty or not:

public boolean isEmpty(Path path) throws IOException {
    if (Files.isDirectory(path)) {
        try (DirectoryStream<Path> directory = Files.newDirectoryStream(path)) {
            return !directory.iterator().hasNext();
        }
    }

    return false;
}

For non-directory inputs, we’ll return false without even trying to load the directory entries:

Path aFile = Paths.get(getClass().getResource("/notDir.txt").toURI());
assertThat(isEmpty(aFile)).isFalse();

On the other hand, if the input is a directory, we’ll try to open a DirectoryStream to that directory. Then we’ll consider the directory as empty if and only if the first hasNext() method call return false. Otherwise, it’s not empty:

Path currentDir = new File("").toPath().toAbsolutePath();
assertThat(isEmpty(currentDir)).isFalse();

The DirectoryStream is a Closeable resource, so we’re wrapping it inside a try-with-resources block. As we might expect, the isEmpty method returns true for empty directories:

Path path = Files.createTempDirectory("baeldung-empty");
assertThat(isEmpty(path)).isTrue();

Here we’re using the Files.createTempDirectory to create an empty and temporary directory.

3. Using Files.list

As of JDK 8, the Files.list method uses the Files.newDirectoryStream API internally to expose a Stream<Path>. Each Path is an entry inside the given parent directory. Therefore, we can also use this API for the same purpose:

public boolean isEmpty(Path path) throws IOException {
    if (Files.isDirectory(path)) {
        try (Stream<Path> entries = Files.list(path)) {
            return !entries.findFirst().isPresent();
        }
    }
        
    return false;
}

Again, we’re only touching the first entry using the findFirst method. If the returned Optional is empty, then the directory is empty, too.

The Stream is backed by an I/O resource, so we’re making sure to release it appropriately using a try-with-resources block.

4. Inefficient Solutions

Both Files.list and Files.newDirectoryStream will iterate the directory entries lazily. Therefore, they will work with huge directories very efficiently. However, solutions like this will not work well in this scenario:

public boolean isEmpty(Path path) {
    return path.toFile().listFiles().length == 0;
}

This will eagerly load all the entries inside the directory which will be pretty inefficient when dealing with huge directories.

5. Conclusion

In this short tutorial, we got familiar with a few efficient ways to check whether a directory is empty or not.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW