In Java, Files.isReadable(path) returns true when the file exists and the current user may read it. It checks the operating system's permissions without opening the file.
A missing file returns false, so the method also works as a combined "exists and readable" check. A true answer is not a promise: permissions can change, or the file can disappear, before you read it.
A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.isReadable(...), after import java.nio.file.Files;. You never create a Files object.
Syntax
Pass the path; there are no options:
Java
if (Files.isReadable(input)) {
List<String> lines = Files.readAllLines(input);
}
Call
What it does
Files.isReadable(p)
Exists and may be read.
Files.isWritable(p)
Exists and may be written.
Files.isExecutable(p)
Exists and may be executed (or searched, for a folder).
Since Java 7. Folders work too: a readable folder can be listed. Results depend on who runs the program; on Linux and macOS the root user may read almost everything.
You own what you create. The program created prices.csv, so it has read permission by default.
Folders are readable too. For a folder, readable means its entries may be listed.
Missing counts as unreadable. The method returns false rather than throwing for a file that does not exist.
Example 2 — Take Away Read Permission on Linux or macOS
Set the permissions to write-only, check again, then restore them.
Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermissions;
public class IsReadablePosix {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path file = dir.resolve("secret.txt");
Files.write(file, "pin=1234".getBytes(StandardCharsets.UTF_8));
System.out.println("Readable at first: " + Files.isReadable(file));
if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
Files.setPosixFilePermissions(file, PosixFilePermissions.fromString("-w-------"));
System.out.println("After removing read: " + Files.isReadable(file));
Files.setPosixFilePermissions(file, PosixFilePermissions.fromString("rw-------"));
} else {
System.out.println("No POSIX permissions on this system");
}
Files.delete(file);
Files.delete(dir);
}
}
Output
Run with java IsReadablePosix. The first part is the output on Linux or macOS for a normal user (not root); the last part shows Windows, which has no POSIX permissions.
Readable at first: true
After removing read: false
--- On Windows ---
Readable at first: true
No POSIX permissions on this system
How It Works
Readable at the start. A freshly written file can be read by its owner.
Write-only permissions. -w------- keeps only the owner's write bit, so the read check now fails for a normal user.
Root is different. Running as root on Linux would still print true, because root bypasses read permissions; that is why the output is labelled "normal user".
Example 3 — Explain Why a File Cannot Be Read
Use isReadable for a friendly message, but keep a catch for problems that appear after the check.
Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class IsReadableSafeRead {
static String readOrExplain(Path p) {
if (!Files.isReadable(p)) {
return p.getFileName() + ": cannot read (missing or no permission)";
}
try {
return p.getFileName() + ": " + new String(Files.readAllBytes(p), StandardCharsets.UTF_8);
} catch (IOException e) {
return p.getFileName() + ": read failed";
}
}
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path motd = dir.resolve("motd.txt");
Files.write(motd, "Welcome!".getBytes(StandardCharsets.UTF_8));
System.out.println(readOrExplain(motd));
System.out.println(readOrExplain(dir.resolve("absent.txt")));
Files.delete(motd);
Files.delete(dir);
}
}
Output
Run with java IsReadableSafeRead. The output is the same on Windows, macOS, and Linux.
motd.txt: Welcome!
absent.txt: cannot read (missing or no permission)
How It Works
Friendly first. The check catches the common cases early and returns a clear message instead of an exception.
Still defensive. The file could be deleted between the check and the read, so the actual read keeps its own catch.
Two results. motd.txt prints its content, and absent.txt gets the explanation.
Frequently Asked Questions
That the file exists and that the current user has permission to read it, according to the operating system.
No. It only checks access rights, so a later read can still fail.
On Linux and macOS the root user bypasses normal read permissions, and Java reports what the operating system allows.
false, without an exception.
On Linux and macOS use Files.setPosixFilePermissions with a set that has no read bits, such as PosixFilePermissions.fromString("-w-------"). On Windows read access is controlled by access-control lists.