Java文件类boolean isDirectory()方法(带示例)
文件类布尔isDirectory()
软件包java.io.File.isDirectory()中提供了此方法。
此方法用于检查filepath指定的文件是否为目录。
此方法的返回类型为布尔值,即,如果该方法返回true,则该值为true或false,这意味着该文件由filepath表示是目录,否则返回false,因此它不是目录。
如果未授予文件读取权限,则此方法可能会引发异常(即SecurityException)。
语法:
boolean isDirectory(){
}参数:
我们不会在File方法中将任何对象作为参数传递。
返回值:
此方法的返回类型为Boolean,即,与在那种情况下,文件由抽象文件路径指定为目录的情况相比,返回true,否则返回false,因此指定的文件不在目录中。
Java程序演示isDirectory()方法示例
//导入File类,因为我们将使用File类方法
import java.io.File;
//导入Exception类,因为它可能引发
//处理文件时出现异常
import java.lang.Exception;
public class ToCheckFileDirectory {
public static void main(String[] args) {
try {
//指定文件的路径,我们使用双斜杠
//为Windows转义'\'字符序列
File file1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles");
File file2 = new File("C:\\Users\\computer clinic\\OneDrive\\JavaArticles");
//通过使用isDirectory()用于检查
//文件路径是否为目录。
//因为给定的文件路径是目录,所以它返回true。
if (file1.isDirectory())
System.out.println("This filepath " + " " + file1.getAbsolutePath() + " " + "is a directory");
else
System.out.println("This filepath " + " " + file1.getAbsolutePath() + " " + "is not a directory");
//通过使用isDirectory()用于检查
//文件路径是否为目录。 It returns false
//因为给定的文件路径不是目录。
if (file2.isDirectory())
System.out.println("This filepath " + " " + file2.getAbsolutePath() + " " + "is a directory ");
else
System.out.println("This filepath " + " " + file2.getAbsolutePath() + " " + "is not a directory");
} catch (Exception e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}输出结果
D:\Programs>javac ToCheckFileDirectory.java D:\Programs>java ToCheckFileDirectory This filepath C:\Users\computer clinic\OneDrive\Articles is a directory This filepath C:\Users\computer clinic\OneDrive\JavaArticles is not a directory
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿