Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

How to check if a file is hidden in Java

abhilash

What are hidden files?

Hidden files are files that exist on a computer but are not visible when you list or examine the computer. Hidden files are basically used to hide information.

If a file’s name begins with a period character (.), the file is considered hidden in UNIX. If a file isn’t a directory and the DOS hidden property is set, it is deemed hidden in Windows.

Files.isHidden() method

The isHidden() method is a static method in the Files class that checks whether the given file path is a hidden file or not.

The Files class is in the java.nio.file package.

Syntax

public static boolean isHidden(Path path)

Parameters

This method takes one parameter, Path path, which is the file path.

Returns

This method returns true if the file is hidden. Otherwise, it returns false.

Code

In the code below, we create a Path object from the file path, which is passed as argument to the isHidden() method.

If the file is hidden, the program prints File is Hidden. If the file is not hidden, the program prints File is not Hidden.

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

public class Main {
    public static void main(String[] args) throws IOException {
        Path filePath = Paths.get("hiddenFile.txt");
        boolean isHidden = Files.isHidden(filePath);
        System.out.println(isHidden? "File is Hidden": "File is not Hidden");
    }
}

RELATED TAGS

java
communitycreator
RELATED COURSES

View all Courses

Keep Exploring