In this tutorial we would learn how to write a program to check whether a particular file is hidden or not. We would be using isHidden() method of File class to perform this check. This method returns a boolean value (true or false), if file is hidden then this method returns true otherwise it returns a false value.
Here is the complete code:
import java.io.File;
import java.io.IOException;
public class HiddenPropertyCheck
{
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");
}else{
System.out.println("The specified file is not hidden");
}
}
}
More details about isHidden() method from javadoc:
public static boolean isHidden(Path path) throws IOException
Tells whether or not a file is considered hidden. The exact definition of hidden is platform or provider dependent. On UNIX for example a file is considered to be hidden if its name begins with a period character (‘.’). On Windows a file is considered hidden if it isn’t a directory and the DOS hidden attribute is set.
Depending on the implementation this method may require to access the file system to determine if the file is considered hidden.
Parameters:
path – the path to the file to test
Returns:
true if the file is considered hidden
Throws:
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.
Leave a Reply