We are moved to new domain
Click -> www.ehowtonow.com
Thursday, 14 May 2015

How to make read-only file using Java

Following example shows how to make file read-only using file.setReadOnly() method in Java.

setReadOnly
public boolean setReadOnly()
Marks the file or directory named by this abstract pathname so that only read operations are allowed. After invoking this method the file or directory is guaranteed not to change until it is either deleted or marked to allow write access. Whether or not a read-only file or directory may be deleted depends upon the underlying system.
Returns:
true if and only if the operation succeeded; false otherwise
Throws:
SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies write access to the named file

Sample Program
ReadOnlyFile.java
package com.javatutorialcorner.file;

import java.io.File;

public class ReadOnlyFile {

 public static void main(String[] args) {
  File jtcFile = new File("/home/annamalai/workspace/File/jtc.txt");
  System.out.println(jtcFile.setReadOnly());
  System.out.println(jtcFile.canWrite());
 }

}

Result
The above code will produce the following outpu.

true
false

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to make read-only file using Java Rating: 5 Reviewed By: eHowToNow