java.io.File canRead()
Description
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.
package com.javatutorialhq.java.examples;
import java.io.File;
/*
* This example source code demonstrates the use of
* canRead() method of File class.
*
*/
public class FileCanReadExample {
public static void main(String[] args) {
// initialize File object
File file = new File("C:javatutorialhqtest.bat");
// check first if file exists
if(file.exists()){
//check if file can be execute
if(file.canRead(){
System.out.println("user have permission to read the file");
}
else{
System.out.println("File cannot be read");
}
}
else{
System.out.println("File does not exists");
}
}
}
