Java java.nio.file.Files isReadable() Method
java.nio.file.Files.isReadable(Path path) is a practical method in the Java NIO (New I/O) file operation API for checking whether the file at the specified path is readable. This method belongs to java.nio.file.Files utility class, providing a simple way to verify a file's 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
- Return
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 has no read permission
Method characteristics
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
The method performs a non-blocking, instantaneous check that does not actually open the file or perform any I/O operation, so the performance overhead is very small.
Relationship with file existence
If the file does not exist, the method directly returns false. To check file existence separately, you should use Files.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"
)
;
}
}
}
Combine 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
(
"Config file does not exist or is not readable"
)
;
}
Notes
Race condition
Because the file system is dynamic,isReadable() 's result may become invalid immediately after the method returns (e.g., the file is deleted or permissions are changed). Therefore, in critical code, you should be prepared to handle possible IOException.
Symbolic links
If the path points to a symbolic link, the method checks the readability of the final target file, not the link itself.
Permission model
The method checks the permissions of the JVM process, not the current user's permissions. 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 possible subsequent I/O exceptions
- Combine checking methods: Use in combination according to requirements
exists(),isReadable(),isRegularFile()and other methods - Consider using try-with-resources: Use try-with-resources when actually reading files to ensure resource release
Example
Path filePath
=
Paths.
get
(
"important.data"
)
;
if
(
Files.
isReadable
(
filePath
)
)
{
try
(
BufferedReader
reader
=
Files.
newBufferedReader
(
filePath
)
)
{
// Safely read a file
}
catch
(
IOException
e
)
{
// Handle possible exceptions
}
}
By using properly isReadable() method, you can perform a preliminary check before attempting to read a file, improving program robustness and user experience.
Java java.nio.file.Files