VideosMy ProjectsProgramming BlogGallery
  1. Home
  2. Blog
  3. Check if a file exists in Java
Programming Blog

Atom/RSS Feed

java

Check if a file exists in Java

java

To check if the file is is a file in Java you need to do the following

Basic file exists check

fileToCheck.isFile()

File fileToCheck = new File("the files path")
if (fileToCheck.isFile()) {
    // the file exists
}

The better check

Alternatively you can check more explicitly if the file exists and is not a directory

fileToCheck.exists()

fileToCheck.isDirectory()

File fileToCheck = new File("the files path")
if (fileToCheck.exists() && !fileToCheck.isDirectory()) {
// the file exists
}
Join my mailing list