Friday, January 13, 2012

How to check if a File is hidden in Java with Example

This article is quick tip to find hidden files in Java by checking hidden properties of a File or directory from Java Program. File Handling is a crucial topic in java already we have discussed about how to create file in java , how to create directory, how to read write in a text file in Java , how to set permissions on file and some more aspect and we will discuss one more property of file, i.e. hidden property. Now the question arise can we make a file Hidden using our java code or not. So the answer would be No-using java File API  it’s not possible to make any file hidden because it’s a OS or platform dependent property.
·          If we are working in UNIX environment then file is considered as Hidden if its name begins with “.
·          And if we are dealing with Windows environment then a file is hidden if we set file hidden property, and also it’s should not be a directory.

Find hidden files in Java with Example

checking and finding hidden file in javaSo  now we are clear that we can’t write code for making a file hidden directly from Java File API  but what we can do is after making any file hidden depending upon the platform we can check the file property is hidden or not and accordingly we can use that file in our application. isHidden() method is provided on file Class in Java which we will use to test this property.

Syntax of the method:

public static boolean isHidden(Path  path) throws IOException

How To check Hidden property of file in Java-isHidden ()Method

Here we pass the path of the file for which we want to test hidden property and it will return true if it’s a hidden file otherwise will get false value.

Here is complete code example of finding hidden file in Java, we are using file.isHidden() method to check whether a file is hidden or not.


 * @author Javin
public class FileHiddenExample{

       public static void main(String[] args)throws SecurityException ,IOException{

        File file = new File("C:/HiddenTest.txt");
        if (file.isHidden()) {
            System.out.println("This file is Hidden file: ");
        }else {

            System.out.println("File is not Hidden ");

That’s all on how to find hidden files in java  which is quite easy by using isHidden() method. let me know if you know any other way of making a file hidden from Java program and we can include that Example here. I will discuss how we can make a file hidden in Java on next post hide Files from Java program

Further Learning
Java Fundamentals Part 1,2
Java Fundamentals: The Java Language
Core Java, Volume II--Advanced Features

Some other Java Tutorial you may like:

No comments :

Post a Comment