How to Check If A File is Hidden in Java?
File.isHidden()
Tests whether the file named by this abstract pathname is a hidden file. The exact definition of hidden is system-dependent. On UNIX systems, a file is considered to be hidden if its name begins with a period character ('.')
. On Microsoft Windows systems, a file is considered to be hidden if it has been marked as such in the filesystem.
Returns:
true if and only if the file denoted by this abstract pathname is hidden according to the conventions of the underlying platform.
Throws:
SecurityException – If a security manager exists and its SecurityManager.checkRead
(java.lang.String) method denies read access to the file.
package com.crunchify.tutorials; import java.io.File; /** * @author Crunchify.com */ public class CrunchifyIfFileHidden { public static void main(String[] args) { File file = new File("/Users/<username>/Documents/log.txt"); if(file.isHidden()){ System.out.println("This file is hidden"); }else{ System.out.println("This file is not hidden"); } } }