如何在 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