How to check if a File exists in Java with Example (original) (raw)

Hello guys, if you are working in Java for a couple of years, then you have come across a scenario where you need to check if a file exists or not. This is often required while writing defensive code that writes or reads from a file. While there is a method to check if a file exists in Java or not, it's not straightforward to use because it returns true if a given path represents a file or a directory. Yes, I am talking about the File. Exists () method of the java.io package. This method is the key method to solve this problem, but you need to keep in mind that it returns true if a given path exists and it's either file or directory.

If your requirement is not just to check if the path is valid, but it also represents a file, and you have permission to read the file, you also need to ensure that it's not a directory.

If you are looking for a code example to check if a file exists in Java or not, you have come to the right place. In this article, I will show you a Java program, and we will see how to check if a File exists or not.

It's a common requirement to first check if a file with input pathname exists or not and if there is no File, then create a new file, but before that, you need to do this check. Earlier, I have shared how to check if a file is empty or not, and today, I will share whether a file exists.

While File.exists() looks alright to solve this problem, there is another gem that many Java developers don't know; yes, I am talking about the File.isFile() method, which checks if a file exists also ensures that its a file. This is actually a better way to check if a file exists in Java or not.

Btw, I am expecting that you are familiar with Java syntax and Java API in general. If you are a complete beginner, then I suggest you first go through these free Java online courses to learn more about core Java basics and such gems from Java API.

How to check if a File exists in Java with Example

Java Example to check if File exists in Java or not

Following is my sample Java program to check if a File exists in the directory or not. The absolute or relative path of a file is provided as input to the program, and it will tell you if that file exists in a file system or not.

import java.io.File;

/**

} Output: File exits in File System - Java check successful Provided path is a file and it exists

That's all on How to check if a File or directory exists or not from the Java program. You can use this code to check if a file exists or not before starting to write into it or open it for any operation. Just keep in mind that the File.exists() method returns true if the given path is either file or directory, so make sure that you also check for that condition, as shown in this example.

A better way is to use the isFile() method, which returns true if this instance is represented by a file and it exists.

Further Learning
10 Things Java Developers Should Learn
10 Books Java Developers Can Read

Thanks for reading this article so far. If you like this article, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.