How to Check if a File is hidden in Java

We can use the isHidden() method of the File class in Java to check whether the file is hidden in Java. This method returns a boolean value-true or false.

Syntax:

public static boolean isHidden(Path path) throws IOException
parameters:
path – the path to the file to test.
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.
returns:
true: if file is hidden 
false: if file is not hidden

The precise definition of hiding depends on the platform or provider.

UNIX: If the file name starts with a period character ("."), the file is hidden.

Windows: If the file is not a directory and the DOS hidden attribute is set, the file is hidden.

Depending on the implementation, the isHidden() method may need to access the file system to determine whether 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");
	else
	System.out.println("The specified file is not hidden");
}
}

 

Submit Your Programming Assignment Details