Check File Exists and Check File Permissions in Java

This is a thorough guide to perform File Exists Check or Directory Exists Check and File Permissions Check using Java IO and Java NIO API.

Overview

In Java there are two ways to perform File Input Output Operations. The traditional way is to use Plain Java IO API and the the new way is to use the Java NIO API. This tutorial covers both Java IO and Java NIO ways of doing file existence check and file permissions check.

First, we will begin by understanding How to check file and folder or directory existence. Then, we will move further to understand how to check a File permissions. In the last part we will cover Why the file File existence checks are required.

File Exists Check

This section covers both the old and the new way of checking if a file exists in the given location.

Using Java IO

With Java IO File class, we can refer to any file on the disk. To check if a File exists, we can invoke exists method provided by File. Note that we are also asserting that file is not a directory.

private boolean checkIfFileExists(String location) { File file = new File(location); assert file.isFile(); return file.exists(); }
Code language: Java (java)

Using Java NIO

The Java NIO package it the latest JavaIO API introduced in Java 7. Its Files class provides a number of static methods, which are useful for general purpose FileIO operations.

Next is the same method which uses Java NIO to check existence of a File.

private boolean checkIfFileExists(String location) { Path path = Path.of(location); assert !Files.isDirectory(path); return Files.exists(path); }
Code language: Java (java)

Directory Exists Check

Java uses the term File to refer to both a file and a directory. Hence the File class instance can refer any path which can be a file or a folder. Let’s quickly see how to Check if a directory is present.

Using Java IO

Here is a legacy Java IO way of checking if a directory exits.

private boolean checkIfDirectoryExists(String location) { File directory = new File(location); assert directory.isDirectory(); return directory.exists(); }
Code language: Java (java)

Using Java NIO

Similarly, we can use Java NIO to first assert that the file is actually a directory and it exists as well.

private boolean checkIfDirectoryExists(String location) { Path path = Path.of(location); assert Files.isDirectory(path); return Files.exists(path); }
Code language: Java (java)

File Permission Check

The previous sections explained how to see if a file or directory is actually present on the given path. Now we will check if a file as correct permissions

With Java IO

As, shown in next block, we can use Java IO to check if a file is writable.

private boolean chekFileIsWritable(String location) throws IOException { File file = new File(location); assert file.isFile() && file.exists(); return file.canWrite(); }
Code language: Java (java)

We first, ensure that the file is actually a file and it exists. The method will return true, if the application has write permissions on the file.

Similarly we can check if file has read permissions.

file.canRead();
Code language: Java (java)

Or, if the file is an executable and has permissions to execute.

file.canExecute();
Code language: Java (java)

With Java NIO

Similarly, we can use Java NIO to check if a File has read permission, write permission, and/or execute permissions.

Path path = Path.of(location); Files.isReadable(path); Files.isWritable(path); Files.isExecutable(path);
Code language: Java (java)

Why should I check if File or Directory Exists?

There are two reasons that Checking If a File Exists or a Directory Exists is important.

Firstly, when we create a File instance using Java IO or NIO the instance will be created even if the provided path is invalid.

In Java IO

File file = new File(location);
Code language: Java (java)

Or, in Java NIO

Path path = Path.of(location); File file = path.toFile();
Code language: Java (java)

However, when we want to read such a file or create an InputSteram on the file, we get FileNotFoundException or NoSuchFileException depending upon the API we use.

Secondly, the existence check can be part of the systems logic. For example, the component you are writing can depend on a file created by another part of the system and want to consume the file as soon as it is created. Thus, your component can repeatedly check if the file exists and proceed as soon as it is available.

Summary

This article explained How to find if a file or a directory exists, as well as if a file or directory has got read, write and execute permissions. It covered legacy way of using plain Java IO, as well as using the new way of Java NIO API. Also, it briefly covered why the file existence check is required. For more on Java, please visit Java Tutorials.