Java File setReadOnly() Method
Example
Make a file read-only:
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.setReadOnly());
System.out.println(myFile.canWrite());
myFile.setWritable(true); // Make the file writable again
}
}
Definition and Usage
The setReadOnly() method marks a file or directory as read-only. The program can still read it, but not change it.
Use setWritable(true) to make it writable again. The example does this at the end, so that the file can be changed later.
Syntax
public boolean setReadOnly()
Parameter Values
None.
Technical Details
| Returns: | true if the file or directory was marked as read-only, otherwise false. |
|---|
Related Pages
❮ File Methods