import java.io.File;
public class $ {
public static void main(String[] args) {
File f = new File("d:/a.txt");
System.out.println(f.exists());
}
}
false.不存在
true.存在
File类中有个方法public boolean exists()
测试此抽象路径名表示的文件或目录是否存在。
返回:
当且仅当此抽象路径名表示的文件或目录存在时,返回 true;否则返回 false
if(new File("d:/test.txt").exists()){
System.out.println("文件已经存在");
}
file.exists()