Home Java Files Methods isRegularFile() Java Files isRegularFile() Method Definition In Java, Files.isRegularFile(path) returns true when path exists and is an ordinary file holding data, even if it is empty. Folders, missing paths, and special entries such as devices or pipes give false.
It is the right check before reading a path someone gave you, and Files::isRegularFile is the usual filter for "files only" when walking a tree.
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.isRegularFile(...), after import java.nio.file.Files;. You never create a Files object.
Syntax Pass the path, and optionally NOFOLLOW_LINKS:
Files.isRegularFile(dir.resolve("data.csv")); // true
Files.isRegularFile(dir); // false
tree.filter(Files::isRegularFile) // files only Call What it does Files.isRegularFile(p)Ordinary file? (follows links.) Files.isRegularFile(p, NOFOLLOW_LINKS)An ordinary file itself, not a link to one? Files.isDirectory(p)The folder counterpart.
Since Java 7. On Linux and macOS some paths are neither files nor folders (for example /dev/null); both checks return false for them.
Library declarations public static boolean isRegularFile(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 regular file. falseFolder, special file, missing, or unknown.
Compare with Similar Methods Related checks:
Java Examples 1. What counts as a file 2. Count files in a tree 3. Validate an input path Example 1 — An Empty File Is Still a Regular File Compare a new empty file, a file with data, a folder, and a missing name.
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class IsRegularFileKinds {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path empty = Files.createFile(dir.resolve("empty.txt"));
Path full = dir.resolve("full.txt");
Files.write(full, "data".getBytes(StandardCharsets.UTF_8));
System.out.println("empty.txt: " + Files.isRegularFile(empty));
System.out.println("full.txt: " + Files.isRegularFile(full));
System.out.println("temp folder: " + Files.isRegularFile(dir));
System.out.println("ghost.txt: " + Files.isRegularFile(dir.resolve("ghost.txt")));
Files.delete(empty);
Files.delete(full);
Files.delete(dir);
}
} Run with java IsRegularFileKinds. The output is the same on Windows, macOS, and Linux.
empty.txt: true
full.txt: true
temp folder: false
ghost.txt: false How It Works Size does not matter. empty.txt has 0 bytes and full.txt has 4; both are regular files.Folders are excluded. The temp folder exists, but it is a directory, so the answer is false.Nothing there. ghost.txt does not exist and also gives false.Example 2 — Count Files and Folders in a Tree Walk a small tree twice: once counting regular files, once counting folders.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class IsRegularFileCount {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path inner = Files.createDirectories(dir.resolve("a").resolve("b"));
Files.createFile(dir.resolve("one.txt"));
Files.createFile(inner.resolve("two.txt"));
Files.createFile(inner.resolve("three.txt"));
long files;
long folders;
try (Stream<Path> tree = Files.walk(dir)) {
files = tree.filter(Files::isRegularFile).count();
}
try (Stream<Path> tree = Files.walk(dir)) {
folders = tree.filter(Files::isDirectory).count();
}
System.out.println("Regular files: " + files);
System.out.println("Folders, including the start: " + folders);
List<Path> all;
try (Stream<Path> tree = Files.walk(dir)) {
all = tree.sorted(Comparator.reverseOrder()).collect(Collectors.toList());
}
for (Path p : all) {
Files.delete(p);
}
}
} Run with java IsRegularFileCount. The output is the same on Windows, macOS, and Linux.
Regular files: 3
Folders, including the start: 3 How It Works The tree. one.txt sits in the temp folder; two.txt and three.txt sit in a/b.A stream is used once. Each count needs a new Files.walk, because a Java stream cannot be traversed twice.The start counts. walk includes the starting folder, so the folders are the temp folder, a, and a/b.Example 3 — Check a Path Before Reading It A helper reports a friendly message for a good file, a folder given by mistake, and a missing name.
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class IsRegularFileValidate {
static String check(Path p) throws IOException {
if (Files.isRegularFile(p)) {
return p.getFileName() + ": ok, " + Files.size(p) + " bytes";
}
if (Files.isDirectory(p)) {
return p.getFileName() + ": is a folder, expected a file";
}
return p.getFileName() + ": not found";
}
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path csv = dir.resolve("data.csv");
Files.write(csv, "a,b\n1,2".getBytes(StandardCharsets.UTF_8));
Path input = Files.createDirectory(dir.resolve("input"));
System.out.println(check(csv));
System.out.println(check(input));
System.out.println(check(dir.resolve("nope.csv")));
Files.delete(csv);
Files.delete(input);
Files.delete(dir);
}
} Run with java IsRegularFileValidate. The output is the same on Windows, macOS, and Linux.
data.csv: ok, 7 bytes
input: is a folder, expected a file
nope.csv: not found How It Works Exact bytes. The text a,b, a newline, and 1,2 is seven characters; writing it as bytes keeps the single \n, so the size is 7 on every system.A common mistake. Users often pass a folder where a file is expected; isRegularFile is false, and isDirectory explains why.Fallback message. If neither check is true, the path is reported as not found.Frequently Asked Questions What is a regular file in Java? An ordinary file that holds data, as opposed to a directory, a symbolic link checked with NOFOLLOW_LINKS, or a special entry such as a device or pipe.
Does Files.isRegularFile return true for empty files? Yes. A file with size 0 is still a regular file.
How do I count the files in a folder tree? Use try (Stream<Path> s = Files.walk(dir)) { long n = s.filter(Files::isRegularFile).count(); }.
Does isRegularFile follow symbolic links? Yes. A link to a file returns true. Pass LinkOption.NOFOLLOW_LINKS to return false for links.
Can a path be neither a file nor a directory? Yes. Missing paths and special entries such as /dev/null on Linux return false for both isRegularFile and isDirectory.
Next: Files.isSameFile() Check whether two paths lead to the same file, even when they are written differently.
isSameFile() tutorial → 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.
10 people found this page helpful
Helpful Share Copy link Suggestion