Check if a File Exists in Java
Problem
Before reading, writing or deleting, programs often need to know whether a path exists and whether it's a regular file or a directory.
Check for a settings file before and after creating it, print its details, then delete it and check again.
Java Program
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class CheckFileExists {
public static void main(String[] args) throws IOException {
Path file = Paths.get("settings.cfg");
System.out.println("Exists before creating: " + Files.exists(file));
Files.writeString(file, "theme=dark");
System.out.println("Exists after creating: " + Files.exists(file));
System.out.println("Is a regular file: " + Files.isRegularFile(file));
System.out.println("Is a directory: " + Files.isDirectory(file));
System.out.println("Size in bytes: " + Files.size(file));
boolean removed = Files.deleteIfExists(file); // no exception if it's already gone
System.out.println("Deleted: " + removed + ", exists now: " + Files.exists(file));
}
}Output
Core Logic
The Files class answers each question about a path with a single static method.
- 1
Files.exists(file)is false before the file is created. - 2
Files.writeStringcreates it, after whichexistsis true. - 3
isRegularFile,isDirectoryandsizedescribe the path. - 4
deleteIfExistsremoves it and returns whether anything was deleted.
theme=dark is 10 characters, so the file is 10 bytes.Key Point: Checking exists() and then acting is a race in multi-user systems — the file can vanish in between; methods like deleteIfExists do the check and the action together.
Key Concepts
Approach 2: Using java.io.File
import java.io.File;
import java.io.IOException;
public class CheckFileExistsIo {
public static void main(String[] args) throws IOException {
File file = new File("settings.cfg");
System.out.println("exists(): " + file.exists());
System.out.println("createNewFile(): " + file.createNewFile()); // true only if it was actually created
System.out.println("exists(): " + file.exists() + ", isFile(): " + file.isFile());
System.out.println("delete(): " + file.delete());
System.out.println("exists(): " + file.exists());
}
}
Output
Core Logic
The older java.io.File API offers the same checks as instance methods that return booleans.
- 1
file.exists()andfile.isFile()describe the path. - 2
createNewFile()returnstrueonly if it actually created the file. - 3
delete()returnstrueon success.
exists() call returns true after createNewFile(), and the last one returns false after delete().Key Point: File.delete() just returns false on failure with no reason given; Files.delete throws an exception that explains what went wrong.
Key Concepts
Interview Questions
Why might both Files.exists and Files.notExists return false?
When the program can't determine the file's status, for example because of missing permissions. Then neither answer is certain.
How do you check if a path is readable?
Files.isReadable(path), and Files.isWritable(path) for writing. They also return false if the file doesn't exist.