arrow_back Back to Notebook
java
java
January 10, 2019

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
}

Thanks for reading!

John Wiseman

Full-Stack Software Engineer building scalable digital solutions. specializing in modern web technologies and mission-critical systems.

© 2026 Wiseman Systems Pty. Ltd.

Navigation

Start a Conversation

Ready to discuss your next project? Let's build something extraordinary together.

Get in Touch