检查文件是否隐藏在Java中
Java中 File 类的 isHidden() 方法可用于检查文件是否隐藏。此方法返回一个布尔值——真或假。
句法:
public static boolean isHidden(Path path)
throws IOException
参数:要测试的文件的路径。
返回类型:布尔值,如果发现文件隐藏则返回 true,否则返回 false,因为未发现隐藏文件
抛出的异常:
- IOException:如果发生 I/O 错误
- SecurityException :在默认提供程序的情况下,并且安装了安全管理器,将调用 checkRead() 方法来检查对文件的读取访问权限。
Remember: Depending on the implementation the isHidden() method may require to access the file system to determine if the file is considered hidden.
例子:
Java
// Java Program to Check if Given File is Hidden or Not
// Using isHidden() Method of File class
// Importing required classes
import java.io.File;
import java.io.IOException;
// Main class
// HiddenFileCheck
public class GFG {
// Main driver method
public static void main(String[] args)
throws IOException, SecurityException
{
// Creating a file by
// creating an object of File class
File file = new File(
"/users/mayanksolanki/Desktop/demo.rtf");
// Checking whether file is hidden or not
// using isHidden() method
if (file.isHidden())
// Print statement as file is found hidden
System.out.println(
"The specified file is hidden");
else
// Print statement as file is found as not
// hidden
System.out.println(
"The specified file is not hidden");
}
}
输出:
输出说明:从输出的背景可以很容易地看到“demo.rtf”文件弹出图标清晰可见。如上所示,该代码反映了特定文件未隐藏在终端输出中。
Note: The precise definition of hidden is a platform or provider-dependent.
- UNIX: A file is hidden if its name begins with a period character (‘.’).
- Windows: A file is hidden if it is not a directory and the DOS hidden attribute is set.