How to check the Existence of a File using Java?

Java 8Object Oriented ProgrammingProgramming

The file class provides a method named exists() which returns true if the file specified in the current file object exists.

Example

Live Demo

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");
     }
   }
}

Output

Given file does not existed
raja
Published on 12-Jan-2018 06:18:35
Advertisements