1. Overview
In this quick tutorial, weβre going to get familiar with different ways to check the existence of a file or directory.
First, weβll start with the modern NIO APIs and then will cover the legacy IO approaches.
2. Using java.nio.file.Files
To check if a file or directory exists, we can leverage the Files.exists(Path) method. As itβs clear from the method signature, we should first obtain a Path to the intended file or directory. Then we can pass that Path to the Files.exists(Path) method:
Path path = Paths.get("does-not-exist.txt");
assertFalse(Files.exists(path));
Since the file doesnβt exist, it returns false. Itβs also worth mentioning that if the Files.exists(Path) method encounters an IOException, itβll return false, too.
On the other hand, when the given file exists, itβll return true as expected:
Path tempFile = Files.createTempFile("baeldung", "exist-article");
assertTrue(Files.exists(tempFile));
Here weβre creating a temporary file and then calling the Files.exists(Path) method.
This even works for directories:
Path tempDirectory = Files.createTempDirectory("baeldung-exists");
assertTrue(Files.exists(tempDirectory));
If we specifically want to know if a file or directory exists, we can also use Files.isDirectory(Path) or Files.isRegularFile(Path) methods:
assertTrue(Files.isDirectory(tempDirectory));
assertFalse(Files.isDirectory(tempFile));
assertTrue(Files.isRegularFile(tempFile));
There is also a notExists(Path) method that returns true if the given Path doesnβt exist:
assertFalse(Files.notExists(tempDirectory));
Sometimes the Files.exists(Path) returns false because we donβt possess the required file permissions. In such scenarios, we can use the Files.isReadable(Path) method to make sure the file is actually readable by the current user:
assertTrue(Files.isReadable(tempFile));
assertFalse(Files.isReadable(Paths.get("/root/.bashrc")));
2.1. Symbolic Links
By default, the Files.exists(Path) method follows the symbolic links. If file A has a symbolic link to file B, then the Files.exists(A) method returns true if and only if the file B exists already:
Path target = Files.createTempFile("baeldung", "target");
Path symbol = Paths.get("test-link-" + ThreadLocalRandom.current().nextInt());
Path symbolicLink = Files.createSymbolicLink(symbol, target);
assertTrue(Files.exists(symbolicLink));
Now if we delete the target of the link, the Files.exists(Path) will return false:
Files.deleteIfExists(target);
assertFalse(Files.exists(symbolicLink));
Since the link target doesnβt exist anymore, following the link wonβt lead to anything, and Files.exists(Path) will return false.
Itβs even possible to not follow the symbolic links by passing an appropriate LinkOption as the second argument:
assertTrue(Files.exists(symbolicLink, LinkOption.NOFOLLOW_LINKS));
Because the link itself exists, the Files.exists(Path) method returns true. Also, we can check if a Path is a symbolic link using the Files.isSymbolicLink(Path) method:
assertTrue(Files.isSymbolicLink(symbolicLink));
assertFalse(Files.isSymbolicLink(target));
3. Using java.io.File
If weβre using Java 7 or a newer version of Java, itβs highly recommended to use the modern Java NIO APIs for these sorts of requirements.
However, to make sure if a file or directory exists in Java legacy IO world, we can call the exists() method on File instances:
assertFalse(new File("invalid").exists());
If the file or directory does exist already, itβll return true:
Path tempFilePath = Files.createTempFile("baeldung", "exist-io");
Path tempDirectoryPath = Files.createTempDirectory("baeldung-exists-io");
File tempFile = new File(tempFilePath.toString());
File tempDirectory = new File(tempDirectoryPath.toString());
assertTrue(tempFile.exists());
assertTrue(tempDirectory.exists());
As shown above, the exists() method doesnβt care if itβs a file or directory. Therefore, as long as it does exist, itβll return true.
The isFile() method, however, returns true if the given path is an existing file:
assertTrue(tempFile.isFile());
assertFalse(tempDirectory.isFile());
Similarly, the isDirectory() method returns true if the given path is an existing directory:
assertTrue(tempDirectory.isDirectory());
assertFalse(tempFile.isDirectory());
Finally, the canRead() method returns true if the file is readable:
assertTrue(tempFile.canRead());
assertFalse(new File("/root/.bashrc").canRead());
When it returns false, the file either doesnβt exist or the current user doesnβt possess the read permission on the file.
4. Conclusion
In this short tutorial, we saw how to make sure a file or directory exists in Java. Along the way, we talked about modern NIO and the legacy IO APIs. Also, we saw how the NIO API handles symbolic links.
