Article
home/blog/Loading

Check if a file exists in 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
}