如何在 Java 中檢查檔案是否存在?


File 類提供了  exists() 方法,該方法如果指定路徑中的檔案存在則返回 true,否則返回 false。

示例

線上演示

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

更新日期:2019 年 7 月 30 日

346 次瀏覽

啟動你的 職業

完成課程獲得認證

首次使用
廣告
© . All rights reserved.