Check if a file exists in Java
This post will discuss how to check if a file exists in Java.
When you are testing a file’s existence, three results are possible:
- The file exists.
- The file doesn’t exist.
- The file’s status is unknown as the program does not have access to the file.
There are several ways to check for a file’s existence in Java. Each of the following solutions returns true if the file exists; false otherwise, when the file doesn’t exist or the file’s status is unknown.
1. Using File.exists()
method
The idea is to use the File.exists()
method to determine whether a file denoted by a specified pathname exists. This method returns true if the file exists; false otherwise.
Note that File.exists()
returns true when your path points to a directory. So, it is recommended to call this method along with the File.isDirectory()
method, which checks for a directory. This is demonstrated below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.io.File; class Main { // Method to check if the file exists and is not a directory public static boolean isFileExists(File file) { return file.exists() && !file.isDirectory(); } public static void main(String[] args) { String filePath = "C:\\doc.txt"; File file = new File(filePath); if (isFileExists(file)) { System.out.println("File exists!!"); } else { System.out.println("File doesn't exist or program doesn't have access " + "to the file"); } } } |
Please note that when operating on NFS-mounted volumes, java.io.File.exists
sometimes returns false even though the file referenced actually does exist. See bug details here.
2. Using File.isFile()
method
We have seen that File.exists()
returns true if your path points to a directory. To explicitly avoid checking for a directory, it is recommended to use File.isFile()
method instead of File.exists()
method. The File.isFile()
method tests whether the file denoted by the specified path is a normal file, i.e., the file is not a directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.io.File; class Main { // Method to check if the file exists public static boolean isFileExists(File file) { return file.isFile(); } public static void main(String[] args) { String filePath = "C:\\doc.txt"; File file = new File(filePath); if (isFileExists(file)) { System.out.println("File exists!!"); } else { System.out.println("File doesn't exist or program doesn't have access " + "to the file"); } } } |
3. Using NIO
From Java 7 onward, we can use java.nio.file.Files
, which provides several static methods that operate on files, directories, or other types of files. To simply check for a file’s existence, we can use exists()
and notExists()
method of java.nio.file.Files
class. The exists()
method returns true if the file exists, whereas the notExists()
method returns true when it does not exist. If both exists()
and notExists()
return false, the existence of the file cannot be verified. This can happen when the program does not have access to the file.
Note that Files.exists()
returns true when your path points to a directory. So, it is recommended to use this method along with the Files.isDirectory()
method, which checks the file for a directory. This is demonstrated below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; class Main { public static void main(String[] args) { String filePath = "C:\\doc.txt"; Path path = Paths.get(filePath); boolean exists = Files.exists(path); boolean notExists = Files.notExists(path); boolean isDir = Files.isDirectory(path); if (isDir) { System.out.println("File is a Directory"); } else if (exists) { System.out.println("File exists!!"); } else if (notExists) { System.out.println("File doesn't exist!!"); } else { System.out.println("Program doesn't have access to the file!!"); } } } |
Alternatively, we can check if a path is a regular file (and not a directory) using the Files.isRegularFile()
method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; class Main { public static void main(String[] args) { String filePath = "C:\\doc.txt"; Path path = Paths.get(filePath); boolean exists = Files.isRegularFile(path); if (exists) { System.out.println("File exists!!"); } else { System.out.println("File doesn't exist!!"); } } } |
That’s all about determining whether a file exists in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)