如何在 Java 中检查文件是否存在?
File 类提供了 exists() 方法,该方法如果指定路径中的文件存在则返回 true,否则返回 false。
示例
import java.io.File;
public class FileHandling {
public static void main(String args[]) {
File file = new File("samplefile");
if(file.exists()) {
System.out.println("Given file existed");
} else {
System.out.println("Given file does not existed");
}
}
}输出
Given file does not existed
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP