Java Examples - Detect if File Exists
The following example demonstrates how to use the file.exists() method of the File class to detect whether a file exists:
Main.java file
import java.io.File;
public class Main {
public static void main(String[] args) {
File file = new File("C:/java.txt");
System.out.println(file.exists());
}
}
The output of running the above code is (if the file java.txt exists on your C drive):
trueOther Extensions
Java Examples