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.

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Sailendra Jena
11 years ago

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

Hung
10 years ago
Reply to  Sailendra Jena

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

Sailendra Jena
10 years ago
Reply to  Hung

Hi Nguyen,

Thanks i have tried again this program its working fine.

Thanks & Regards
Sailendra Narayan Jena