如何使用 Java 检查文件是否存在?
文件类提供了一个名为 exists() 的方法,如果文件对象中指定的当前文件存在,则该方法会返回 true。
示例
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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP