Wednesday, May 24, 2023

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

This article is a quick tip to find hidden files in Java by checking the hidden properties of a File or directory from the Java Program. File Handling is a crucial topic in java already we have discussed how to create a file in java, how to create a directory, how to read write in a text file in Java, how to set permissions on the file, and some more aspects and we will discuss one more property of the file, i.e. hidden property. Now the question arises 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 an OS or platform-dependent property.

If we are working in the UNIX environment then the file is considered as Hidden if its name begins with “.

And if we are dealing with the Windows environment then a file is hidden if we set file hidden property, and also it’s should not be a directory.

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



How to Find hidden files in Java with Example

So 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

checking and finding hidden file in javaHere 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 a false value.

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

import java.io.File;

/**
 * @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 the 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


Some other Java Tutorial you may like:

2 comments :

Shramik said...

We can set hidden attribute for DOS based OS (Windows) as below using NIO2 API

Path path = Paths.get("c:\sample");
Files.createFile(path);
Files.setAttribute(path, "dos:hidden", true);

or

DosFileAttributeView dosFileAttrView = Files.getFileAttributeView(path, DosFileAttributeView.class);
dosFileAttrView.setHidden(true);

We also can read this addribute using NIO2 API.

javin paul said...

Thanks Shramik for this useful information and adding value to the post.

Post a Comment