How do I check if a file exists in Java

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

How can I check whether a file exists, before opening it for reading in Java? (equivalent of Perl s -e $filename).

http://stackoverflow.com/questions/1237235/check-file-exists-java http://stackoverflow.com/questions/1237235/check-file-exists-java

If possible I d prefer a real API call returning true/false as opposed to some "Call API to open a file and catch when it throws an exception which you check for no file in text", but I can live with the latter.

Answers

Using java.io.File:

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

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/1816673/how-do-i-check-if-a-file-exists-in-java

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils