Java Check if file exists
In this article, we show how to check if a file exists in Java. First, we talk about what is the java.io.File.exists() method and how you can use it. Then we analyze how to check if a file is readable, writable, and executable.
1. Introduction
The Files.exists() and Files.notExists() are methods that test if a file or a directory exists defined by its pathname. Furthermore the methods Files.isReadable(path), Files.isWritable(path), Files.isExecutable(path) help us to check if we can read, write or execute a file.
2. Technologies Used
The example code in this article was built and run using:
- Java 1.8.231(1.8.x will do fine)
- Eclipse IDE for Enterprise Java Developers- Photon
3. Check if file exists
In this section we can analyse how we can check if a file exists or not.
3.1. Files.Exists()
With this example in Java, we are going to demonstrate how to check if a File exists. We are using the File class that is an abstract representation of file and directory pathnames. In short, to check if a File exists you should:
- Create a new File instance by converting the given pathname string into an abstract pathname.
- Use
exists()
API method of File. This method tests whether the file or directory denoted by this abstract pathname exists. It returns true if and only if the file or directory denoted by this abstract pathname exists and false otherwise.
Let’s take a look at the code snippet that follows:
import java.io.File; public class CheckIfFileExists { public static void main(String[] args) { File file = new File("C://file.txt"); // Tests whether the file denoted by this abstract pathname exists. boolean exists = file.exists(); System.out.println("File " + file.getPath() + " exists: " + exists); } }
The output is:
File C:\file.txt exists: false
3.2. Files.notExists()
With this example in Java, we are going to demonstrate how to check if a File doesn’t exist. If we use the guidelines that we said before we create an example:
import java.io.File; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class CheckIfFileNotExist { public static void main(String[] args) { Path path = Paths.get("d:\\my-test-file.txt"); boolean b = Files.notExists(path); System.out.println(b); } }
The output is:
true
4. Check file rights
In this section, we will check if a file is readable, writable, or executable. Particularly, these methods help us to check whether The JVM has the privileges that allow it to open the file and reading it, writing it, executing it, or not.
4.1. Files.isReadable(path)
Here we can see an example for Files.isReadable(path) to check if a file is readable :
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; public class Read { public static void main(String[] args) throws IOException { Path temp1 = Files.createTempFile("temp1", ".txt"); System.out.println("The path of the file: " + temp1); System.out.println( Files.isReadable(temp1)); } }
The output is:
The path of the file: C:\Users\Mcris\AppData\Local\Temp\temp15434597760525503172.txt true
4.2. Files.isWritable(path)
Here we can see an example for Files.isWritable(path) to check if a file is writable :
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; public class Write { public static void main(String[] args) throws IOException { Path temp1 = Files.createTempFile("temp2", ".txt"); System.out.println("The path of the file: " + temp1); System.out.println( Files.isWritable(temp1)); } }
The output is:
The path of the file: C:\Users\Mcris\AppData\Local\Temp\temp2186047962998960243.txt true
4.3. Files.isExecutable(path)
Here we can see an example for Files.isExecutable(path) to check if a file is executable :
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; public class Execute { public static void main(String[] args) throws IOException { Path temp3 = Files.createTempFile("temp3", ".txt"); System.out.println("The path of the file: " + temp3); System.out.println( Files.isExecutable(temp3)); } }
The output is:
The path of the file: C:\Users\Mcris\AppData\Local\Temp\temp32566576210242349507.txt true
5. Download the Source Code
You can download the full source code of this example here: Java Check if file exists
Last updated on Jun. 01st, 2020