How do I check if a file exists in Java?

How can I check whether a file exists, before opening it for reading in Java? (equivalent of Perl's -e $filename).
Last updated:8/3/2015 1:10:09 AM

1 Answers

Mayank Tripathi
Mayank Tripathi

Use java.io :

File f = new File(filePathString);
if(f.exists() && !f.isDirectory()) { /* do something */ }

Answer