Home Tutorial Java Core Files How to make a file read-only in java

 
 

Share on Google+Share on Google+
How to make a file read-only in java
Posted on: February 16, 2005 at 12:00 AM
Advertisement
In this section, you will learn how to make a file read only.

How to make a file read-only in java

A read-only file is any file with the read-only attribute. Read-only files can be opened and accessed but you will not be able to make changes to it. They can be deleted and moved. You can make a file read-only by using file.setReadOnly() method of File class. After using this method, you will not be able to write or edit into the file. 

Here is the code:

import java.io.File;

public class FileReadOnly {
  public static void main(String[] args) {
    File file = new File("c:/file.txt");
    file.setReadOnly();
    System.out.println("File is in read only mode");
    }
}

The above program will make a the file read only

To make file read only file.setReadOnly(); method is used.

In the page you learned how to make a file read only.

Advertisement

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


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: February 16, 2005

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com