Java File setReadable() Method
Example
Turn on the read permission of a file, then check it:
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.setReadable(true));
System.out.println(myFile.canRead());
}
}
Definition and Usage
The setReadable() method turns the read permission (the right to read the file) on or off.
On some systems, like Windows, Java cannot turn the read permission off. There, setReadable(false) returns false, and the file can still be read.
Tip: Use the canRead() method to check if the file can be read.
Syntax
One of the following:
public boolean setReadable(boolean readable)
public boolean setReadable(boolean readable, boolean ownerOnly)
Parameter Values
| Parameter | Description |
|---|---|
| readable | Required. true to allow reading, false to stop reading. |
| ownerOnly | Optional. If true (the default), the permission is changed only for the owner of the file. If false, it is changed for all users. If the system cannot tell the owner from other users, it is changed for all users. |
Technical Details
| Returns: | true if the permission was changed, otherwise false. |
|---|
Related Pages
❮ File Methods