Java java.nio.file.Files isReadable() Method
java.nio.file.Files.isReadable(Path path)is a utility method in the Java NIO (New I/O) file operation API, used to check whether the file at the specified path is readable. This method belongs tojava.nio.file.Filesutility class, providing a simple way to verify file readability without actually opening the file.
Method Definition
Parameter Description
path: The file path to check, of typejava.nio.file.Path
Return Value
- Returns
booleanType:true: If the file exists and the current JVM has permission to read the filefalse: If the file does not exist or the current JVM does not have read permission
Method Features
Security Check
isReadable()The method performs a security check to verify whether the current Java Virtual Machine has permission to read the file at the specified path. This is different from directly checking whether the file exists, because even if the file exists, it may be unreadable due to permission restrictions.
Non-blocking Operation
This method performs a non-blocking, instantaneous check. It does not actually open the file or perform any I/O operations, so the performance overhead is very small.
Relationship with File Existence
If the file does not exist, the method directly returnsfalse. To check whether the file exists separately, use theFiles.exists()method.
Usage Examples
Basic Usage
Example
public class IsReadableExample {
public static void main(String[] args) {
Path filePath = Paths.get("example.txt");
if (Files.isReadable(filePath)) {
System.out.println("File is readable");
} else {
System.out.println("File is not readable or does not exist");
}
}
}
Combining with Other Checks
Example
if (Files.exists(filePath) && Files.isReadable(filePath)) {
// Safely read file content
List<String> lines = Files.readAllLines(filePath);
// Process file content...
} else {
System.err.println("Configuration file does not exist or is not readable");
}
Precautions
Race Condition
Since the file system is dynamic,isReadable()the result may become invalid immediately after the method returns (for example, the file is deleted or permissions are changed). Therefore, in critical code, you should be prepared to handle exceptions that may occur when actually reading the file later.IOException。
Symbolic Links
If the path points to a symbolic link, this method checks the readability of the final target file, not the link itself.
Permission Model
This method checks the permissions of the JVM process, not the permissions of the current user. In special security environments (such as when SecurityManager is enabled), the result may be affected.
Related Methods
Files.exists(Path path): Check whether the file existsFiles.isWritable(Path path): Check whether the file is writableFiles.isExecutable(Path path): Check whether the file is executableFiles.isRegularFile(Path path): Check whether it is a regular file (not a directory, symbolic link, etc.)
Best Practices
- **Do not rely solely on isReadable()**: Always be prepared to handle I/O exceptions that may occur later.
- Combine check methods: Combine and use as needed
exists(),isReadable(),isRegularFile()and other methods - Consider using try-with-resources: Use try-with-resources when actually reading the file to ensure resources are released
Example
if (Files.isReadable(filePath)) {
try (BufferedReader reader = Files.newBufferedReader(filePath)) {
// Safely read the file
} catch (IOException e) {
// Handle possible exceptions
}
}
By making reasonable use of theisReadable()method, you can perform preliminary checks before attempting to read a file, improving program robustness and user experience.
Java java.nio.file.Files