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