HOW DO I CHECK IF A FILE EXISTS IN JAVA?

Mikki Halpin

Total Post:23

Points:161
Posted by  Mikki Halpin
Java  File 
 712  View(s)
Ratings:
Rate this:
How can I check whether a file exists, before opening it for reading in Java? (equivalent of Perl's -e $filename).
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: How do I check if a file exists in Java?

    Use java.io :

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!