Making a file read-only

Use the method setReadOnly in the File class. Notice that there is no method to determine whether a file is read-only or not, neither is there a method to undo the setReadOnly operation:

Main.java:

import java.io.*;
   
public class Main {
   public static void main(String[] args) {
      File file = new File("Main.java");
      boolean status = file.setReadOnly(); 
      System.out.println(file + " " + (status ? "":"un") + "successfully set to read-only");
   }
}

When you execute (Windows DOS-prompt) attrib main.java, you’ll see:

  A    R     MAIN~1.JAV    C:Main.java