Java File canRead() Method
Example
Check if a file can be read:
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
File myFile = new File("filename.txt");
myFile.createNewFile();
System.out.println(myFile.canRead());
}
}
Definition and Usage
The canRead() method checks if the program is allowed to read the file.
It returns false if the file does not exist, or if the program does not have permission (the right) to read it.
Tip: Use the setReadable() method to change the read permission.
Syntax
public boolean canRead()
Parameter Values
None.
Technical Details
| Returns: | true if the file exists and can be read, otherwise false. |
|---|
Related Pages
❮ File Methods