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