Check If a File Exists in Java



The File class provides exists() method this returns true if a file in the specified path exists else it returns false.

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
Updated on: 2019-07-30T22:30:20+05:30

375 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements