Java Examples - File Existence

Advertisements


Problem Description:

How to check a file exist or not?

Solution:

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

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

Result:

The above code sample will produce the following result (if the file "java.txt" exists in 'C' drive).

true


Advertisements
Advertisements