Monday 14, Apr 2025
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
01.package com.javatutorialcorner.file;
02. 
03.import java.io.File;
04. 
05.public class ReadOnlyFile {
06. 
07. public static void main(String[] args) {
08.  File jtcFile = new File("/home/annamalai/workspace/File/jtc.txt");
09.  System.out.println(jtcFile.setReadOnly());
10.  System.out.println(jtcFile.canWrite());
11. }
12. 
13.}

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

Related Posts:

  • How to make read-only file using Java Following example shows how to make file read-only using file.setReadOnly() method in Java. setReadOnlypublic boolean setReadOnly() Marks the file or directory named by this abstract pathname so that only read operations a… Read More
  • How to write file using FileOutputStream in JavaIn this tutorials we are going to see how to write file using FileOutputStream in Java. 1.Create project called JavaMisc. 2.Create package called com.javatutorialscorner.io Create java class called FileWriter under c… Read More
  • How to change File Last modified Date using Java Following example shows how to change file last modified date using file.setLastModified() method in Java. setLastModifiedpublic boolean setLastModified(long time)  Sets the last-modified time of the file or directory … Read More
  • How to Create File using Java Following example shows hoe to create file using File() constructor and file.createNewFile() method in Java createNewFile public boolean createNewFile() throws Atomically creates a new, empty file named by this abstract pa… Read More
  • How to check file existence in Java Following example shows how to check file existence using file.exists() method in Java. existspublic boolean exists() Tests whether the file or directory denoted by this abstract pathname exists. Returns:    … Read More
  • 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