Change a file attribute to read only in Java
Description
The following code shows how to change a file attribute to read only.
Example
//from w w w . ja v a2s .com
import java.io.File;
public class Main {
public static void main(String[] args) throws Exception {
File file = new File("ReadOnly.txt");
file.createNewFile();
file.setReadOnly();
if (file.canWrite()) {
System.out.println("writable!");
} else {
System.out.println("read only!");
}
}
}
The code above generates the following result.