如何使用 Java 检查文件是否存在?
File 类提供了一个名为 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