Files.exists() / isFile() / isDirectory() / createDirectory()
Methods for checking the existence and type of files and directories, as well as creating and deleting them. Use the Files class together with Path. Checking for existence before performing file operations helps prevent unnecessary exceptions.
Syntax
// Checks whether a file or directory exists.
Files.exists(Path.of("path"));
// Checks whether the path points to a regular file.
Files.isFile(Path.of("path"));
// Checks whether the path points to a directory.
Files.isDirectory(Path.of("path"));
// Creates a directory, including any missing intermediate directories.
Files.createDirectories(Path.of("path"));
// Deletes a file.
Files.delete(Path.of("path"));
Method List
| Method | Description |
|---|---|
| Files.exists(Path) | Checks whether the file or directory at the given path exists, and returns a boolean. |
| Files.isRegularFile(Path) | Checks whether the path points to a regular file. For symbolic links, the link target is checked. |
| Files.isDirectory(Path) | Checks whether the path points to a directory, and returns a boolean. |
| Files.createDirectory(Path) | Creates a single directory. Throws an exception if the parent directory does not exist. |
| Files.createDirectories(Path) | Creates a directory along with any necessary intermediate directories. Does not throw an exception if the directory already exists. |
| Files.delete(Path) | Deletes a file or an empty directory. Throws an exception if the path does not exist. |
| Files.deleteIfExists(Path) | Deletes a file only if it exists. Does not throw an exception if the path does not exist. |
Sample Code
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Path filePath = Path.of("sample.txt");
Path dirPath = Path.of("output/data");
// Check whether the file exists.
if (Files.exists(filePath)) {
System.out.println("The file exists.");
} else {
System.out.println("The file does not exist.");
}
// Determine whether the path is a file or a directory.
System.out.println(Files.isRegularFile(filePath)); // true if it is a regular file.
System.out.println(Files.isDirectory(dirPath)); // true if it is a directory.
// Create a directory, including any intermediate directories.
try {
Files.createDirectories(dirPath);
System.out.println("Directory created.");
} catch (IOException e) {
e.printStackTrace();
}
// Delete the file.
try {
Files.deleteIfExists(filePath);
System.out.println("Deletion complete (does nothing if the file does not exist).");
} catch (IOException e) {
e.printStackTrace();
}
Notes
The "check-then-act" pattern — checking with exists() before performing an operation — is susceptible to a race condition known as TOCTOU (Time-Of-Check Time-Of-Use). In a multithreaded environment, another process may create or delete the file between the check and the operation. It is recommended to handle such cases with exception handling rather than relying on existence checks.
delete() cannot remove a non-empty directory. To delete a directory along with its contents, you need to combine it with Files.walk() and delete recursively.
If you find any errors or copyright issues, please contact us.