Java Files isRegularFile() Method

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

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:

Java
Files.isRegularFile(dir.resolve("data.csv"));   // true
Files.isRegularFile(dir);                         // false
tree.filter(Files::isRegularFile)                 // files only
CallWhat 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
Java
public static boolean isRegularFile(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 regular file.
falseFolder, special file, missing, or unknown.

Compare with Similar Methods

Related checks:

MethodHow it differs
isDirectory()True for folders.
exists()True for any kind of entry.
size()How many bytes the file holds.

Java Examples

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.

Java
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);
    }
}

How It Works

  1. Size does not matter. empty.txt has 0 bytes and full.txt has 4; both are regular files.
  2. Folders are excluded. The temp folder exists, but it is a directory, so the answer is false.
  3. 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.

Java
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);
        }
    }
}

How It Works

  1. The tree. one.txt sits in the temp folder; two.txt and three.txt sit in a/b.
  2. A stream is used once. Each count needs a new Files.walk, because a Java stream cannot be traversed twice.
  3. 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.

Java
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);
    }
}

How It Works

  1. 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.
  2. A common mistake. Users often pass a folder where a file is expected; isRegularFile is false, and isDirectory explains why.
  3. Fallback message. If neither check is true, the path is reported as not found.

Frequently Asked Questions

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.
Yes. A file with size 0 is still a regular file.
Use try (Stream<Path> s = Files.walk(dir)) { long n = s.filter(Files::isRegularFile).count(); }.
Yes. A link to a file returns true. Pass LinkOption.NOFOLLOW_LINKS to return false for links.
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

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.

10 people found this page helpful