首頁  >  文章  >  Java  >  java讀取檔案是否存在的方法

java讀取檔案是否存在的方法

尚
原創
2019-12-05 15:17:541852瀏覽

java讀取檔案是否存在的方法

以下實例示範了使用File 類別的file.exists() 方法來偵測檔案是否存在:

import java.io.File;

public class Main {
   public static void main(String[] args) {
      File file = new File("C:/java.txt");
      System.out.println(file.exists());
   }
}

執行結果為(如果你的C 磁碟中存在檔案java.txt):

true

exists():

public boolean exists()測試此抽象路徑名稱表示的檔案或目錄是否存在。

回傳:當且僅當此抽象路徑名表示的檔案或目錄存在時,傳回true;否則回傳false;

拋出:SecurityExcepTIon如果存在安全管理器,且其SecurityManager .checkRead(java.lang.String)方法拒絕對檔案或目錄進行寫入存取。

更多java知識請關注java基礎教學欄。

以上是java讀取檔案是否存在的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn