Java Files isHidden() Method

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

Definition

In Java, Files.isHidden(path) returns true if the file counts as hidden on the current system. The rule is not the same everywhere.

On Linux and macOS a name that starts with a dot, such as .env, is hidden. On Windows the name does not matter: a file is hidden when its DOS hidden attribute is set, for example with Files.setAttribute(p, "dos:hidden", true).

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.isHidden(...), after import java.nio.file.Files;. You never create a Files object.

Syntax

Pass the path; it may throw IOException:

Java
boolean hidden = Files.isHidden(dir.resolve(".env"));
// Linux/macOS: true    Windows: false (no hidden attribute)
CallWhat it does
Files.isHidden(p)Hidden according to the platform's rule.
Files.setAttribute(p, "dos:hidden", true)Hides a file on Windows.
p.getFileName().toString().startsWith(".")The Unix rule written by hand.

Since Java 7. On Linux the check looks only at the name, so it works even for files that do not exist. On Windows the attributes must be read from disk. On macOS some newer JDK versions also honour the Finder "hidden" flag.

Library declarations
Java
public static boolean isHidden(Path path) throws IOException

Parameters

ParameterDescription
pathThe file to test.

Return Value

Return ValueDescription
trueThe file is hidden on this system.
falseIt is not hidden.

Compare with Similar Methods

Related helpers:

MethodHow it differs
setAttribute()Sets dos:hidden on Windows.
newDirectoryStream()Lists entries, hidden ones included.
readAttributes()Reads DOS attributes in bulk.

Java Examples

Example 1 — Is .env Hidden?

Check a dot file and a normal file. The answer depends on the operating system.

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

public class IsHiddenDotFile {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path env = Files.createFile(dir.resolve(".env"));
        Path props = Files.createFile(dir.resolve("app.properties"));
        System.out.println(".env hidden: " + Files.isHidden(env));
        System.out.println("app.properties hidden: " + Files.isHidden(props));
        Files.delete(env);
        Files.delete(props);
        Files.delete(dir);
    }
}

How It Works

  1. The Unix rule. On Linux and macOS a leading dot is what hides a file, so .env is hidden and app.properties is not.
  2. The Windows rule. Windows ignores the dot; neither file has the hidden attribute, so both answers are false.
  3. Portable code, portable meaning. The same line of Java asks "would this system's file manager hide it?", not "does the name start with a dot?".

Example 2 — List Only Visible Files

Leave hidden entries out of a listing. A loop is used because isHidden throws a checked exception.

Java
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class IsHiddenVisible {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        String[] names = {".cache.db", "photo.jpg", "notes.txt"};
        for (String n : names) {
            Files.createFile(dir.resolve(n));
        }
        List<String> visible = new ArrayList<>();
        try (DirectoryStream<Path> entries = Files.newDirectoryStream(dir)) {
            for (Path p : entries) {
                if (!Files.isHidden(p)) {
                    visible.add(p.getFileName().toString());
                }
            }
        }
        Collections.sort(visible);
        System.out.println("Visible: " + visible);
        for (String n : names) {
            Files.delete(dir.resolve(n));
        }
        Files.delete(dir);
    }
}

How It Works

  1. A plain loop. Files::isHidden declares IOException, which a stream filter cannot throw, so a DirectoryStream loop is simpler.
  2. Hidden entries skipped. On Linux and macOS .cache.db starts with a dot and is left out.
  3. Sorted for a stable line. Collections.sort fixes the order; on Windows all three names stay, and the dot sorts first.

Example 3 — isHidden on a Missing Path

Linux decides by name alone; Windows has to read attributes and fails for a missing file.

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

public class IsHiddenMissing {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path ghost = dir.resolve(".ghost");
        System.out.println(".ghost exists: " + Files.exists(ghost));
        try {
            System.out.println(".ghost hidden: " + Files.isHidden(ghost));
        } catch (NoSuchFileException e) {
            System.out.println(".ghost hidden: cannot tell, NoSuchFileException");
        }
        Files.delete(dir);
    }
}

How It Works

  1. Nothing on disk. .ghost was never created, which exists confirms.
  2. Name-only on Linux. The dot is enough, so Linux returns true without touching the disk.
  3. Attribute-based on Windows. Windows must read the hidden attribute, cannot find the file, and throws NoSuchFileException, which the program catches.

Frequently Asked Questions

It follows the platform rule: on Linux and macOS, a name starting with a dot; on Windows, the DOS hidden attribute.
Windows ignores the dot. A file is hidden only when its hidden attribute is set.
Call Files.setAttribute(path, "dos:hidden", true). On Linux and macOS rename the file so its name starts with a dot.
On some systems, such as Windows, it has to read file attributes, which fails for missing or inaccessible files.
Not directly, because it throws a checked IOException. Use a loop, or wrap the call in a lambda that catches the exception.

Next: Files.isReadable()

Check whether a file exists and this program is allowed to read it.

isReadable() 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.

8 people found this page helpful