Java Files isDirectory() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

In Java, Files.isDirectory(path) returns true when path exists and is a folder. For a regular file, a missing path, or a path that cannot be checked it returns false.

Because it takes a Path and returns a boolean, the method reference Files::isDirectory works directly as a stream filter.

A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.isDirectory(...), after import java.nio.file.Files;. You never create a Files object.

Syntax

Pass the path, and optionally NOFOLLOW_LINKS:

Java
Files.isDirectory(dir);                                // true
Files.isDirectory(dir.resolve("notes.txt"));            // false
stream.filter(Files::isDirectory)                       // keep folders only
CallWhat it does
Files.isDirectory(p)Folder? (follows a link to a folder.)
Files.isDirectory(p, NOFOLLOW_LINKS)Folder itself, not a link to one?
Files.isRegularFile(p)The matching test for files.

Since Java 7. It replaces java.io.File.isDirectory(). When you need several attributes at once, readAttributes reads them in one go.

Library declarations
Java
public static boolean isDirectory(Path path, LinkOption... options)

Parameters

ParameterDescription
pathThe path to test.
optionsLinkOption.NOFOLLOW_LINKS to treat a link as a link.

Return Value

Return ValueDescription
trueIt exists and is a directory.
falseNot a directory, missing, or unknown.

Compare with Similar Methods

Type checks:

MethodHow it differs
isRegularFile()True for normal files.
isSymbolicLink()True for the link itself.
createDirectories()Makes the folder if the check says it is missing.

Java Examples

Example 1 — Ask Three Paths Whether They Are Folders

Only the real folder answers true.

Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class IsDirectoryKinds {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path music = Files.createDirectory(dir.resolve("music"));
        Path song = Files.createFile(music.resolve("song.mp3"));
        System.out.println("music: " + Files.isDirectory(music));
        System.out.println("song.mp3: " + Files.isDirectory(song));
        System.out.println("videos: " + Files.isDirectory(dir.resolve("videos")));
        Files.delete(song);
        Files.delete(music);
        Files.delete(dir);
    }
}

How It Works

  1. A real folder. music was created with createDirectory, so it answers true.
  2. Files are not folders. song.mp3 exists, but it is a regular file.
  3. Missing means false. videos was never made; there is no exception, just false.

Example 2 — List Just the Subfolders

Use Files::isDirectory as a filter on Files.list, then sort, because listing order is not fixed.

Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class IsDirectoryFilter {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path music = Files.createDirectory(dir.resolve("music"));
        Path docs = Files.createDirectory(dir.resolve("docs"));
        Path notes = Files.createFile(dir.resolve("notes.txt"));
        List<String> folders;
        try (Stream<Path> entries = Files.list(dir)) {
            folders = entries.filter(Files::isDirectory)
                             .map(p -> p.getFileName().toString())
                             .sorted()
                             .collect(Collectors.toList());
        }
        System.out.println("Folders: " + folders);
        Files.delete(notes);
        Files.delete(docs);
        Files.delete(music);
        Files.delete(dir);
    }
}

How It Works

  1. Three entries. The temp folder holds two subfolders and one file.
  2. Method reference as a filter. Files::isDirectory fits filter because it takes a Path and returns a boolean; notes.txt is dropped.
  3. Sort the survivors. The names are sorted alphabetically, so docs comes before music on every system.

Example 3 — Reuse, Create, or Refuse an Output Folder

A helper reuses an existing folder, creates a missing one, and refuses when a file is in the way.

Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class IsDirectoryPrepare {
    static String prepare(Path out) throws IOException {
        if (Files.isDirectory(out)) {
            return "reusing " + out.getFileName();
        }
        if (Files.exists(out)) {
            return out.getFileName() + " is a file, pick another name";
        }
        Files.createDirectories(out);
        return "created " + out.getFileName();
    }

    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path out = dir.resolve("out");
        Path clash = Files.createFile(dir.resolve("out.txt"));
        System.out.println(prepare(out));
        System.out.println(prepare(out));
        System.out.println(prepare(clash));
        Files.delete(clash);
        Files.delete(out);
        Files.delete(dir);
    }
}

How It Works

  1. Missing folder. On the first call out is neither a folder nor anything else, so the helper creates it.
  2. Existing folder. The second call finds a directory and reuses it.
  3. Wrong kind. out.txt exists but isDirectory is false, so the helper refuses instead of crashing later.

Frequently Asked Questions

Call Files.isDirectory(path). It returns true only if the path exists and is a folder.
Yes. A link to a folder returns true. Pass LinkOption.NOFOLLOW_LINKS to get false for links.
false. It does not throw an exception.
Use try (Stream<Path> s = Files.list(dir)) { s.filter(Files::isDirectory)... } and sort the result if order matters.
They answer the same question. Files.isDirectory works with Path and supports LinkOption.NOFOLLOW_LINKS.

Next: Files.isExecutable()

Check whether a file may be run as a program, or a folder may be entered.

isExecutable() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful