如何使用 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