File System Operations in Java
Fast recall
Quick revision
Core answer
Files.createFile or createDirectories to create entries, Files.exists or attribute reads to inspect a snapshot, and Files.delete or deleteIfExists to remove an entry. Prefer performing the intended operation and handling its exception over relying on a separate check.Interview-ready guide
Interview answer
Modern Java file operations use Path with the Files class. Files.createFile(path) creates one empty file and normally throws FileAlreadyExistsException if the name is already taken. Files.createDirectory(path) creates one directory, while Files.createDirectories(path) also creates missing parent directories and accepts an already existing target directory.
Files can be checked with exists, isRegularFile, isDirectory, or readAttributes. These checks describe only that moment. Another program may change the path immediately afterward, so if (!exists) createFile is unsafe; call createFile directly and handle its result.
For example, an export job can call createDirectories(outputDirectory) and then createFile(outputFile). If another job already claimed that file name, the exception gives a clear conflict instead of overwriting data. Cleanup can use Files.deleteIfExists(path) when a missing file is acceptable. Files.delete(path) instead reports a missing path as an error, and a directory normally must be empty.
A file system is shared external state, so every operation can fail because of permissions, storage, links, or another process. Deleting a symbolic-link path removes the link itself rather than its target. Perform the exact operation, choose replacement and link behavior deliberately, and treat earlier existence or permission checks as information rather than guarantees.
File System Operations in Java
Direct Answer
Use Files.createFile or createDirectories to create entries, Files.exists or attribute reads to inspect a snapshot, and Files.delete or deleteIfExists to remove an entry. Prefer performing the intended operation and handling its exception over relying on a separate check.
Deep Dive & Architecture
A sequence such as if (!Files.exists(path)) Files.createFile(path) has a gap. Another process or thread can create, replace, or link that name after the check. The safe primitive is the creation operation itself, which either creates the entry or reports why it could not.
The same issue applies to permission checks. isWritable cannot promise that a later open will succeed. Open or create the resource with the intended options, catch the relevant exception, and decide whether to retry, choose another name, or report failure.
Recursive deletion is a traversal problem, not a property of Files.delete; children must be handled before their directory.
Code Example
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
class FileOperations {
public static void main(String[] args) throws Exception {
Path root = Files.createTempDirectory("exports-");
Path output = root.resolve("report.txt");
try {
Files.createDirectories(root);
try {
Files.createFile(output);
Files.writeString(output, "complete");
} catch (FileAlreadyExistsException ex) {
System.out.println("Another job claimed the report");
}
} finally {
Files.deleteIfExists(output);
Files.deleteIfExists(root);
}
}
}
Check your understanding
Answer these without looking back at the article.
- What is the difference between createDirectory and createDirectories?
- Why is Files.exists not a guarantee?
- What happens when Files.delete receives a symbolic link?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.