Java java.nio.file.Files isReadable() Method

Java File Java java.nio.file.Files


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

public static boolean isReadable(Path path)

Parameter Description

  • path: The file path to check, of typejava.nio.file.Path

Return Value

  • ReturnsbooleanType:
    • true: If the file exists and the current JVM has permission to read the file
    • false: 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

import java.nio.file.*;

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

Path filePath = Paths.get("data/config.properties");

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 exists
  • Files.isWritable(Path path): Check whether the file is writable
  • Files.isExecutable(Path path): Check whether the file is executable
  • Files.isRegularFile(Path path): Check whether it is a regular file (not a directory, symbolic link, etc.)

Best Practices

  1. **Do not rely solely on isReadable()**: Always be prepared to handle I/O exceptions that may occur later.
  2. Combine check methods: Combine and use as neededexists(), isReadable(), isRegularFile()and other methods
  3. Consider using try-with-resources: Use try-with-resources when actually reading the file to ensure resources are released

Example

Path filePath = Paths.get("important.data");

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 File Java java.nio.file.Files

Other Extensions