Java File setWritable() Method
Example
Turn off the write permission of a file, then turn it on again:
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();
myFile.setWritable(false);
System.out.println(myFile.canWrite());
myFile.setWritable(true);
System.out.println(myFile.canWrite());
}
}
Definition and Usage
The setWritable() method turns the write permission (the right to change the file) on or off.
Use true to allow changes, and false to make the file read-only.
Tip: Use the canWrite() method to check if the file can be changed.
Syntax
One of the following:
public boolean setWritable(boolean writable)
public boolean setWritable(boolean writable, boolean ownerOnly)
Parameter Values
| Parameter | Description |
|---|---|
| writable | Required. true to allow changes, false to stop changes. |
| 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