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

更新日期: 20-Feb-2020

108 次瀏覽

開啟您的 職業

完成課程,透過認證

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