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

更新日期:2020 年 2 月 20 日

108 次瀏覽

開啟你的職業生涯

完成課程,獲得認證

開始學習
廣告
© . All rights reserved.