Java check if file exists
In this article, we will take a look at two different ways to check if a file or folder/directory exists at a location using java api.
java.io.File
class has an exists()
method which returns true
if the file is present at the path to which the file object points and false
if the actual file does not exists.File object is created using its constructor which takes the path of file as argument. Example,
# create a file object File file = new File("D:/names.txt"); if(file.exists()) { System.out.print("File is present"); } else { System.out.print("File is not present"); }
exists()
method will return true
even if the file path represents a directory or a folder.
NIO was added in java 7 and
java.nio.Files
is one of the classes added in java.nio
package. This class contains static methods to operate on files.This means that in order to use the methods of
Files
class, you are not required to create an object of this class.Files
class has an exists()
method which can check if the file exists or not.exists()
method takes an object of java.nio.file.Path
as argument, which represents the actual location of the file.Path object can be created using
Paths.get()
method providing it the location of file as a string. Example,
// create path object Path path = Paths.get("D:/names.txt"); boolean isPresent = Files.exists(path); if(isPresent) { System.out.print("File is present"); }else { System.out.print("File is not present"); }
Files.exists()
will check for the presence of both files and folders.
If you want to test for a file or folder particularly, then use Files.isRegularFile()
and Files.isDirectory()
methods respectively.
Java doc for isRegularFile
()
method states,
returns
true
if the file is a regular file;false
if the file does not exist, is not a regular file, or it cannot be determined if the file is a regular file or not.
Java doc for isDirectory()
method states,
returns
true
if the file is a directory;false
if the file does not exist, is not a directory, or it cannot be determined if the file is a directory or not.
A symbolic link means a reference to the actual file. A symbolic link can be compared to a shortcut on Windows operating system.
It is not an actual file itself, but points to a file. A symbolic link to a file can be created using
createSymbolicLink()
method of Files
class, passing it the path of link and actual file path as
// create a path to symbolic link Path linkPath = Paths.get("link"); // create symbolic link Path symbolicLink = Files.createSymbolicLink(linkPath, Paths.get("D:/names.txt"));
Files.exists()
method returns true
for a symbolic link if the actual file exists.
Now, suppose, if the actual file is deleted, then you can choose if the exists()
method checks for the actual file or not.
exists()
method accepts a second parameter which is an option whether you want to check for the actual file existence when the file path is a symbolic link.
By default, it checks for the actual file but this can be turned off using LinkOption.NOFOLLOW_LINKS
as shown below
// create a path to symbolic link Path linkPath = Paths.get("link"); // create symbolic link Path symbolicLink = Files.createSymbolicLink(linkPath, Paths.get("D:/names.txt")); boolean exists = Files.exists(symbolicLink, LinkOption.NOFOLLOW_LINKS); if(isPresent) { System.out.print("File is present"); } else { System.out.print("File is not present"); }
In this example, a symbolic link is supplied to exists()
method.
Now, even if the actual file is deleted, exists()
method will still return true
because we have asked not to follow link using LinkOption.NOFOLLOW_LINKS
.
Second argument to exists()
is a var arg parameter. You can choose not to provide it.