共計 391 個字符,預計需要花費 1 分鐘才能閱讀完成。
在 Java 中,可以使用 File
類的 exists()
方法來判斷文件是否存在。以下是一個示例代碼:
import java.io.File;
public class FileExistsExample {public static void main(String[] args) {
String filePath = "C:/example.txt";
File file = new File(filePath);
if (file.exists()) {System.out.println("文件存在");
} else {System.out.println("文件不存在");
}
}
}
在上面的示例中,我們創建了一個 File
對象,并通過調用 exists()
方法檢查文件是否存在。如果文件存在,則輸出 ” 文件存在 ”,否則輸出 ” 文件不存在 ”。請確保將 filePath
變量設置為您要檢查的文件的路徑。
丸趣 TV 網 – 提供最優質的資源集合!
正文完