Skip to main content

Use Java to check if a file exists

We can check if a file exists or not by using exists() method of File object.
String filePathString = "C:\\test.txt";

File f = new File(filePathString);
if ( f.exists() ) {
    System.out.println("File exists.");
} else {
    System.out.println("File does not exist.");
}

Comments