The File Object has an exists methods that returns true if a file exists.
file.exists();
Here an example of checking if a file exists in Java. The code first creates a File Object with the file path; then it calls the method exists to that returns true if the file exists. Finally, depending in the value of the boolean returned, a message is written in the output.
import java.io.File;
public class FileExistsJava {
public static void main(String[] argv){
File file = new File( "V:/tmp/test.txt" );
if ( file.exists() )
System.out.println( "The file exists." );
else
System.out.println( "The file doesn't exist." );
}
}
The file exists.