Check if a File is hidden in Java

We can check a file is hidden or not in Java using isHidden() method of File class in Java. This method returns a boolean value – true or false.


public static boolean isHidden(Path path) throws IOException
path – the path to the file to test.
IOException – if an I/O error occurs
SecurityException – In the case of the default provider, 
and a security manager is installed, the checkRead method
is invoked to check read access to the file.
true: if file is hidden 
false: if file is not hidden

The precise definition of hidden is platform or provider dependent.
UNIX: A file is hidden if its name begins with a period character (‘.’).
Windows: A file is hidden if it is not a directory and the DOS hidden attribute is set.

Depending on the implementation the isHidden() method may require to access the file system to determine if the file is considered hidden.

// Java program to check if the given 
// file is hidden or not
import java.io.File;
import java.io.IOException;
public class HiddenFileCheck
  public static void main(String[] args)
         throws IOException, SecurityException
    // Provide the complete file path here
    File file = new File("c:/myfile.txt");
    if (file.isHidden())
      System.out.println("The specified file is hidden");
      System.out.println("The specified file is not hidden");

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment



load comments

Subscribe to Our Newsletter