java.io.File canRead()

Description

On this document we will be showing a java example on how to use the canRead() method of File Class. This method is basically a check if the file can be read. On some platforms it may be possible to start the Java virtual machine with special privileges that allow it to read files that are marked as unreadable. Consequently this method may return true even though the file does not have read permissions.

This method returns SecurityException – If a security manager exists and its SecurityManager.checkExec(java.lang.String) method denies read access to the file

Method Syntax

public boolean canRead()

Method Argument

Data Type Parameter Description
N/A N/A N/A

Method Returns

This method returns boolean, true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise.

Compatibility

Requires Java 1.0 and up

Java File canRead() Example

Below is a java code demonstrates the use of canRead() method of File class. The example presented might be simple however it shows the behaviour of the canRead() method. Basically we initialize a new File object that corresponds to a physical file which does have permission for reading and then the canExecute() method is used to check if the input file can be read.

Sample Output

Below is the sample output when you run the above example.

java lang File canRead() example output

Suggested Reading List

References