How to check if a file is hidden in Java

A Java program to demonstrate the use of java.io.File isHidden() to check if a file is hidden.


package com.mkyong;

import java.io.File;
import java.io.IOException;

public class FileHidden
{
	
    public static void main(String[] args) throws IOException
    {	
    	File file = new File("c:/hidden-file.txt");
    	
    	if(file.isHidden()){
    		System.out.println("This file is hidden");
    	}else{
    		System.out.println("This file is not hidden");
    	}
    }
}
Note
The isHidden() method is system dependent, on UNIX platform, a file is considered hidden if it’s name is begins with a “dot” symbol (‘.’); On Microsoft Windows platform, a file is considered to be hidden, if it’s marked as hidden in the file properties.

3 comments on “How to check if a file is hidden in Java

  1. Hi MK,

    I tried this program but this program is showing correct answer as is not an hidden file while i am not keeping my file as hidden but while i am keeping my file as hidden the its showing me same answer as file is not hidden where actually file is in hidden. Can you please tell me why this issue coming. Its not showing correct answer.

    Thanks
    Sailendra Narayan Jena

    Reply
    1. Hi, My friend

      I think , You should try it on again. I see in Java. If you are use Eclip or NetBeans. It’s not same. Yes, File isHidden() to check if a file is hidden, so you should try it on. The code in here only compile program in java, I think you should read so more. I had try & success it, Good luck

      Best,
      Nguyen Viet Hung

      Reply
      1. Hi Nguyen,

        Thanks i have tried again this program its working fine.

        Thanks & Regards
        Sailendra Narayan Jena

        Reply

Leave a Comment

Your email address will not be published. Required fields are marked *