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