Home Tutorial Java Corejava How to make a file read-only

 
 

Share on Google+Share on Google+
How to make a file read-only
Posted on: April 21, 2010 at 12:00 AM
Advertisement
This tutorial demonstrate how to create file and set its attribute read-only

Description:

This example demonstrate how to create a file and how to revoke the write permission of the same file. The method createFile creates a file with a specified name and setWritable(false) revoke the write persmission.. The method canWrite() returns booleasn value which whether the file is write-able or not..

Code:

import java.io.File;
import java.io.IOException;

public class FileAttributes {
  public static void main(String[] argsthrows IOException {
    File file = new File("newfile.txt");
    if (file.exists()) {
      file.delete();
    }
    file.createNewFile();
    System.out.println("writable : " + file.canWrite());
    file.setWritable(false);
    System.out.println("writable : " + file.canWrite());
  }
}

Output:

Note : Execution of this program will create a file with specified name with read-only attribute. You can also check the the file properties, by default it is read-only.

Advertisement

Related Tags for How to make a file read-only:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 21, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com