File类判断功能的方法

exists()        测试此抽象路径名表示的文件或目录是否存在。

isDirectory()    测试此抽象路径名表示的文件是否是一个目录。

isFile()        测试此抽象路径名表示的文件是否是一个标准文件。

import java.io.File;

public class Demo04File {
    public static void main(String[] args) {
        //判断路径是否存在
        show01();
        show02();
        show03();
    }

    private static void show03() {
        File f1=new File("C:\Users\cy\Desktop\1.jpg");
        File f2=new File("C:\Users\cy\Desktop");
        boolean file1 = f1.isFile();
        boolean file2 = f2.isFile();
        System.out.println(file1);
        System.out.println(file2);
    }

    private static void show02() {
        File f1=new File("C:\Users\cy\Desktop\1.jpg");
        File f2=new File("C:\Users\cy\Desktop");
        boolean directory1 = f1.isDirectory();
        boolean directory2 = f2.isDirectory();
        System.out.println(directory1);
        System.out.println(directory2);
    }

    private static void show01() {
        File f1=new File("C:\Users\cy\Desktop\1.jpg");
        File f2=new File("C:\Users\cy\Desktop\2.jpg");
        boolean exists1 = f1.exists();
        boolean exists2 = f2.exists();
        System.out.println(exists1);
        System.out.println(exists2);
    }
}
原文地址:https://www.cnblogs.com/cy2268540857/p/13767191.html