Java - File Input Output File Existence

Introduction

You can check if the abstract pathname of a File object exists using the exists() method.

Demo

import java.io.File;

public class Main {
  public static void main(String[] args) throws Exception {
    // Create a File object
    File myFile = new File("dummy.txt");

    // Check for the file's existence
    boolean fileExists = myFile.exists();
    if (fileExists) {
      System.out.println("The dummy.txt file exists.");
    } else {/*from   w  ww .  j a v  a  2  s.c o  m*/
      System.out.println("The dummy.txt file does not exist.");
    }

  }
}

Result

Related Topic