Before trying perform any actions on a file or directory it is prudent to make sure that the file or directory exists. If it doesn't and an application attempts to do something with a non-existent file or directory then things are going to fall apart pretty quickly.
The Path interface can be used as a representation of a path on the file system but even if a
Path interface variable holds a path it does not mean that the path actually exists. Don't confuse the representation of a path with an actual one. To verify the existence of a file or directory use the
The Files Class
Files class was introduced in Java 7 along with the
Path interface. It's filled with static methods for performing operations on files and directories.
Note: As the methods are static you do not need an instance of the
Files class to use them.
Verify the Existence of a File or Directory
You might think that a file or directory either exists or it doesn't. There is also a third possibility. An application might not be able to verify if a file or directory exists because it does not have access to the file. Another thing to consider is that between checking to see if a file exists and using/creating the file things might have changed. There is no guarantee that there hasn't been a change on the file system. In the majority of cases this isn't a problem but it is something to note.
There are two methods for checking the existence of a file -
notExists. If we have a
Path to a file:
Path p = Paths.get("/Users/writing/Desktop/Pics/Untitled56.txt");
we can check if it exists:
boolean doesExist = Files.exists(p);
or check if it does not exist:
Boolean doesExist = Files.notExists(p);
Remember that if both the exists and
notExists methods return
false then the existence of file cannot be confirmed.
Both methods can also accept an option which determines if they follow symbolic links or not:
boolean doesExist = Files.exists(p, LinkOption.NOFOLLOW_Links);
Checking Access to a File
Files class has three methods which can be used to check the access an application has to a file -
Path p = Paths.get("/Users/writing/Desktop/Pics/Untitled56.txt"); boolean isFileReadable = Files.isReadable(p); boolean isFileExecutable = Files.isExecutable(p); boolean isFileWritable = Files.isWritable(p);
Checking for the Same File
With the use of symbolic links on a filesystem it's possible to have two valid paths pointing to the same file. If you want to check if two paths are pointing to the same location use the
Path p = Paths.get("/Users/writing/Desktop/Pics/Untitled56.txt"); Path alias = Paths.get("/Users/writing/Desktop/Untitled.txt" ); boolean istheSameFile = Files.isSameFile(p, alias);
Other Useful Checking Methods
There are other methods which can be useful with a Path:
isHidden- check to see if a file is hidden. You might need to check what an operating system considers to be a hidden file.
isSymbolicLink- tests to see if a
Pathis a symbolic link.
isDirectory- checks to see if the Path is a directory rather than a file.