Home Java Files Methods isHidden() Java Files isHidden() Method 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:
boolean hidden = Files.isHidden(dir.resolve(".env"));
// Linux/macOS: true Windows: false (no hidden attribute) Call What 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 public static boolean isHidden(Path path) throws IOException Parameters Parameter Description pathThe file to test.
Return Value Return Value Description trueThe file is hidden on this system. falseIt is not hidden.
Important
Throws IOException when attributes must be read and cannot be, for example NoSuchFileException for a missing file on Windows (Example 3). Because it throws a checked exception, Files::isHidden cannot be used directly in a stream filter. On Windows, older JDKs report folders as never hidden.
Compare with Similar Methods Related helpers:
Java Examples 1. Dot files 2. Skip hidden files 3. Files that do not exist Example 1 — Is .env Hidden? Check a dot file and a normal file. The answer depends on the operating system.
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);
}
} Run with java IsHiddenDotFile. The first part is the Linux/macOS output; the last part shows Windows.
.env hidden: true
app.properties hidden: false
--- On Windows ---
.env hidden: false
app.properties hidden: false How It Works The Unix rule. On Linux and macOS a leading dot is what hides a file, so .env is hidden and app.properties is not.The Windows rule. Windows ignores the dot; neither file has the hidden attribute, so both answers are false.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.
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);
}
} Run with java IsHiddenVisible. The first part is the Linux/macOS output; the last part shows Windows.
Visible: [notes.txt, photo.jpg]
--- On Windows ---
Visible: [.cache.db, notes.txt, photo.jpg] How It Works A plain loop. Files::isHidden declares IOException, which a stream filter cannot throw, so a DirectoryStream loop is simpler.Hidden entries skipped. On Linux and macOS .cache.db starts with a dot and is left out.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.
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);
}
} Run with java IsHiddenMissing. The first part is the Linux output (macOS behaves the same with typical JDKs); the last part shows Windows.
.ghost exists: false
.ghost hidden: true
--- On Windows ---
.ghost exists: false
.ghost hidden: cannot tell, NoSuchFileException How It Works Nothing on disk. .ghost was never created, which exists confirms.Name-only on Linux. The dot is enough, so Linux returns true without touching the disk.Attribute-based on Windows. Windows must read the hidden attribute, cannot find the file, and throws NoSuchFileException, which the program catches.Frequently Asked Questions How does Java decide if a file is hidden? It follows the platform rule: on Linux and macOS, a name starting with a dot; on Windows, the DOS hidden attribute.
Why is .gitignore not hidden on Windows? Windows ignores the dot. A file is hidden only when its hidden attribute is set.
How do I hide a file on Windows from Java? Call Files.setAttribute(path, "dos:hidden", true). On Linux and macOS rename the file so its name starts with a dot.
Why does Files.isHidden throw IOException? On some systems, such as Windows, it has to read file attributes, which fails for missing or inaccessible files.
Can I use Files::isHidden in a stream filter? Not directly, because it throws a checked IOException. Use a loop, or wrap the call in a lambda that catches the exception.
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.
8 people found this page helpful
Helpful Share Copy link Suggestion