【问题标题】:Efficient way to check if the file exists at start-up [duplicate]在启动时检查文件是否存在的有效方法[重复]
【发布时间】:2015-12-30 19:08:06
【问题描述】:

到目前为止,我提出了这个解决方案,但我想知道是否有更有效的方法来做到这一点。 伪代码:

public static void main(String args[]){
boolean FileNotFound = false;
FileReader file = new FileReader("path");
   try (BufferedReader bReader = new BufferedReader(file){
   //nothing to execute here
   }catch (FileNotFoundException e) {
      FileNotFound = true; 
      }
if (FileNotFound) {
//generate the file
 } 
}

【问题讨论】:

  • File.exists()?
  • 如果你想确保它是一个文件,而不是一个目录,使用File.isFile()

标签: java file


【解决方案1】:

随便用

public static boolean fileExists(String path) {
    File file = new File(path);
    return file.exists();
}

然后只需调用它

...
if(fileExists("path")) ...
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-31
    • 2020-03-23
    • 2011-01-16
    • 1970-01-01
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多