Java Programs›File Handling›Check if a File Exists

Check if a File Exists in Java

beginner·  File Handling  ·  File I/O

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.

Input
settings.cfg
Output
false → true → deleted

Java Program

Java
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

Exists before creating: false Exists after creating: true Is a regular file: true Is a directory: false Size in bytes: 10 Deleted: true, exists now: false

Core Logic

The Files class answers each question about a path with a single static method.

How It Works
  1. 1Files.exists(file) is false before the file is created.
  2. 2Files.writeString creates it, after which exists is true.
  3. 3isRegularFile, isDirectory and size describe the path.
  4. 4deleteIfExists removes 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

Files.exists()Files.isRegularFile()Files.size()Files.deleteIfExists()

Approach 2: Using java.io.File

Java
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

exists(): false createNewFile(): true exists(): true, isFile(): true delete(): true exists(): false

Core Logic

The older java.io.File API offers the same checks as instance methods that return booleans.

How It Works
  1. 1file.exists() and file.isFile() describe the path.
  2. 2createNewFile() returns true only if it actually created the file.
  3. 3delete() returns true on success.
The second 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

java.io.FilecreateNewFile()delete()

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.

Learn the Concept

Related Programs