Mark file or directory Read Only


/*
  Mark file or directory Read Only
  This Java example shows how set mark a particular file or directory as a read only
  using setReadyOnly method of Java File class.
*/

import java.io.*;

public class MarkFileReadOnly {

 
public static void main(String[] args) {
   
   
//create file object
   
File file = new File("C://FileIO//demo.txt");
   
   
/*
     * To mark a particular file or directory as a read only, use
     * boolean setReadyOnly() method of Java File class.
     *
     * This method returns true if the operation was successful.
     */
    
    
boolean blnMarked  = file.setReadOnly();
    
     System.out.println
("Was file marked read only ?: " + blnMarked);
    
    
//check whether file is readonly or not using canWrite method
    
System.out.println("Is file writable ?: " + file.canWrite());
 
}
}

/*
Output would be
Was file marked read only ?: true
Is file writable ?: false
*/