Reshma Bidikar

In this blog post, I will be explaining how you can check if a file exists at a specified path. Consider the following code snippet:


public class FileExists {

public static void main(String args[]){ 
    String fileName = "C:/Test.txt"; 
    File file = new File(fileName); 
    if(file.exists()){ 
        System.out.println("File is present"); 
    } else 
        System.out.println("File is missing"); 
    } 
}

Here the File.exists method is used. This returns a true if a file with the specified name exists at the path specified, otherwise it returns a false.

If you run this code, you will get the following output (Assuming there is a file on C drive called test.txt):

File is present