Skip to content
Related Articles

Related Articles

Check if a File is Hidden in Java

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 18 Apr, 2022
Improve Article
Save Article

isHidden() method of File class in Java can use be used to check if a file is hidden or not. This method returns a boolean value – true or false.

Syntax:

public static boolean isHidden(Path path) 
throws IOException

Parameters: Path to the file to test.

Return Type: A boolean value, true if file is found hidden else returns false as a file is not found hidden 

Exceptions Thrown:

  • 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.

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

Example:

Java




// Java Program to Check if Given File is Hidden or Not
// Using isHidden() Method of File class
 
// Importing required classes
import java.io.File;
import java.io.IOException;
 
// Main class
// HiddenFileCheck
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws IOException, SecurityException
    {
 
        // Creating a file by
        // creating an object of File class
        File file = new File(
            "/users/mayanksolanki/Desktop/demo.rtf");
 
        // Checking whether file is hidden or not
        // using isHidden() method
        if (file.isHidden())
 
            // Print statement as file is found hidden
            System.out.println(
                "The specified file is hidden");
        else
 
            // Print statement as file is found as not
            // hidden
            System.out.println(
                "The specified file is not hidden");
    }
}

Output: 

Output Explanation: As it can easily be visualized from the background of the output that the ‘demo.rtf’ file popping icon is clearly seen. The code reflects that a specific file is not hidden on the terminal output as seen above.

Note: The precise definition of hidden is a 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. 

This article is contributed by Saket Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!