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
Result
The above code will produce the following outpu.
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
0 comments:
Post a Comment