Home Java Files Methods isDirectory() Java Files isDirectory() Method 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:
Files.isDirectory(dir); // true
Files.isDirectory(dir.resolve("notes.txt")); // false
stream.filter(Files::isDirectory) // keep folders only Call What 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 public static boolean isDirectory(Path path, LinkOption... options) Parameters Parameter Description pathThe path to test. optionsLinkOption.NOFOLLOW_LINKS to treat a link as a link.
Return Value Return Value Description trueIt exists and is a directory. falseNot a directory, missing, or unknown.
Compare with Similar Methods Type checks:
Java Examples 1. File, folder, missing 2. Keep only folders 3. Prepare an output folder Example 1 — Ask Three Paths Whether They Are Folders Only the real folder answers true.
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);
}
} Run with java IsDirectoryKinds. The output is the same on Windows, macOS, and Linux.
music: true
song.mp3: false
videos: false How It Works A real folder. music was created with createDirectory, so it answers true.Files are not folders. song.mp3 exists, but it is a regular file.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.
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);
}
} Run with java IsDirectoryFilter. The output is the same on Windows, macOS, and Linux.
Folders: [docs, music] How It Works Three entries. The temp folder holds two subfolders and one file.Method reference as a filter. Files::isDirectory fits filter because it takes a Path and returns a boolean; notes.txt is dropped.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.
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);
}
} Run with java IsDirectoryPrepare. The output is the same on Windows, macOS, and Linux.
created out
reusing out
out.txt is a file, pick another name How It Works Missing folder. On the first call out is neither a folder nor anything else, so the helper creates it.Existing folder. The second call finds a directory and reuses it.Wrong kind. out.txt exists but isDirectory is false, so the helper refuses instead of crashing later.Frequently Asked Questions How do I check if a path is a directory in Java? Call Files.isDirectory(path). It returns true only if the path exists and is a folder.
Does Files.isDirectory follow symbolic links? Yes. A link to a folder returns true. Pass LinkOption.NOFOLLOW_LINKS to get false for links.
What does Files.isDirectory return for a missing path? false. It does not throw an exception.
How do I list only subdirectories? Use try (Stream<Path> s = Files.list(dir)) { s.filter(Files::isDirectory)... } and sort the result if order matters.
Is Files.isDirectory the same as File.isDirectory? They answer the same question. Files.isDirectory works with Path and supports LinkOption.NOFOLLOW_LINKS.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion