Java - Check if a file is hidden

Posted on November 4, 2016


This example shows the use of isHidden() method of the java.io.File class. This method returns true if and only if the file is hidden according to the conventions of the underlying platform; false otherwise.

Here is an example to demonstrate this.

HiddenFileExample.java

package com.boraji.tutorial.io;

import java.io.File;

/**
 * @author imssbora
 * HiddenFileExample.java
 * Nov 4, 2016
 */
public class HiddenFileExample {
	public static void main(String[] args) {
		File file=new File("file.txt");
		/*Check if file exists*/
		if(file.exists()){
			/*Check if file is hidden*/
			if(file.isHidden()){
				System.out.println("File is hidden.");
			}else{
				System.out.println("File is not hidden.");
			}
		}else{
			System.out.println("File does not exist.");
		}
		
	}
}

Output

File is hidden.