使用File类的exists方法判断文件存在
Java中可以通过java.io.File类的exists()方法来检查指定路径的文件是否存在。如果该方法返回true,说明文件存在,否则文件不存在。示例代码如下:
File file = new File("path/to/file.txt");
if (file.exists()) {
System.out.println("文件存在");
} else {
System.out.println("文件不存在");
}
使用File类的exists方法判断文件存在
Java中可以通过java.io.File类的exists()方法来检查指定路径的文件是否存在。如果该方法返回true,说明文件存在,否则文件不存在。示例代码如下:
File file = new File("path/to/file.txt");
if (file.exists()) {
System.out.println("文件存在");
} else {
System.out.println("文件不存在");
}