How to check a file exist or not?


Levels of difficulty: / perform operation:

Problem & Solution

This example shows how to check a file’s existence by using file.exists() method of File class.

JAVA Program

import java.io.File;

public class Main {
   public static void main(String[] args) {
      File file = new File("C:/java.txt");
      System.out.println(file.exists());
   }
}

Output

The above code sample will produce the following result (if the file “java.txt” exists in ‘C’ drive).

True