Java java.nio.file.Files exists() Method
java.nio.file.Files.exists()The method is a very practical utility method in the Java NIO (New I/O) package, used to check whether the file or directory at a specified path exists. In daily development, checking existence before file operations is a common need, andexists()The method provides a simple and efficient solution for this.
Method Definition
Files.exists()The method has the following two overloaded forms:
Basic Form:
Form with LinkOption:
Example
Parameter Description
Path path
- Type:
java.nio.file.Path - Description: The path object of the file or directory to check
- Example:
Path path = Paths.get("example.txt");
LinkOption... options
- Type: Variable-length argument of
LinkOptionenum - Description: Optional parameter that controls how symbolic links are handled
- Optional Values:
LinkOption.NOFOLLOW_LINKS: Do not follow symbolic links
- Default Behavior: If this parameter is not specified, the method follows symbolic links to check the target file
Return Value
- Type:
boolean - Description:
true: If the file/directory existsfalse: If the file/directory does not exist- If it cannot be determined whether the file exists (e.g., due to permission issues), an exception may be thrown
Usage Example
Basic Usage
Example
public class FileExistsExample {
public static void main(String[] args) {
Path filePath = Paths.get("test.txt");
if (Files.exists(filePath)) {
System.out.println("File exists");
} else {
System.out.println("File does not exist");
}
}
}
Check if Directory Exists
Example
if (Files.exists(dirPath)) {
System.out.println("Directory exists");
}
Using LinkOption to Not Follow Symbolic Links
Example
if (Files.exists(symlinkPath, LinkOption.NOFOLLOW_LINKS)) {
System.out.println("Symbolic link exists (link target not checked)");
}
Notes
Concurrency Issues:
- Even if
exists()returnstrue, the file may have been deleted by the time of subsequent operations - For critical operations, it is recommended to perform the operation directly in a try-catch block rather than checking existence first
- Even if
Performance Considerations:
- Multiple calls to
exists()may incur performance overhead - For scenarios with frequent checks, consider caching results or using other mechanisms
- Multiple calls to
Alternative Methods:
Files.notExists()Yesexists()the inverse method of- For cases where you need to create a file, you can use
Files.createFile()method, which throws an exception if the file already exists
Exception Handling:
- The method may throw
SecurityException(if there is no access permission) - Other I/O errors may also cause exceptions
- The method may throw
Comparison with Other Methods
| Method | Description | Difference |
|---|---|---|
File.exists() |
Existence check with java.io.File class | Old API, usually poorer performance |
Files.exists() |
Existence check with NIO | More modern, supports more options |
Files.notExists() |
Check that file does not exist | Opposite logic, but note the "unknown" state |
Best Practices
Avoid Redundant Checks:
Example
if (Files.exists(path)) {
Files.delete(path);
}
// Recommended
try {
Files.delete(path);
} catch (NoSuchFileException e) {
// Handle the case where the file does not exist
}
Combine with Other Checks:
Example
// Handle regular files
}
Correctly Handle Symbolic Links:
Example
boolean linkExists = Files.exists(path, LinkOption.NOFOLLOW_LINKS);
// Check whether the link target exists
boolean targetExists = Files.exists(path);
Summary
Files.exists()The method is a simple but important utility method in Java NIO file operations. It provides a standard way to check the existence of files/directories, supports symbolic link handling options, and is a modern alternative to the traditionalFile.exists()modern solution. In actual use, developers should note its limitations, especially reliability issues in concurrent environments, and consider placing file operations directly in exception handling blocks rather than relying on prior existence checks.
Java java.nio.file.Files