Checking the existence of a file is easy in Java and is a method behind. The file.exists() method in java.io.File does the thing. Here is a sample tutorial.
-----------------------------------
Output
-----------------------------------
The name of the file is myfile.txt
Does the file exist? false
MPVWCWTNM4YU
import java.io.*;f.exists(): Check the existence of a file. If the file is present, it returns true else it returns false.
class FileExistence
{
public static void main(String args[])
{
File f=new File("myfile.txt");
// Print the name
System.out.println("The name of the file is "+f.getName());
// Does the file exist
System.out.println("Does the file exist? "+f.exists());
}
}
-----------------------------------
Output
-----------------------------------
The name of the file is myfile.txt
Does the file exist? false
MPVWCWTNM4YU