Java File.exists()

Syntax

File.exists() has the following syntax.

public boolean exists()

Example

In the following code shows how to use File.exists() method.


/* ww w . j  a  v a2 s.  c om*/
import java.io.File;

public class Main {
  public static void main(String[] args) throws Exception{

    // create new files
    File f = new File("test.txt");

    // create new file in the system
    f.createNewFile();

    // tests if file exists
    boolean bool = f.exists();

    System.out.println("File exists: " + bool);

  }
}

The code above generates the following result.