1. Overview
In this article, We’ll learn how to make a file as read only in java. After creating the file in java, we have to specify the file property readOnly flag to true. But, we can not set this flag to true directly.
File api has a utility method setReadOnly() method which returns a boolean value. True is returned if the file is successfully changed to read only form else false is returned.
Example to convert file from writable to read only and vice-versa.
2. Java Example To Set File As Read Only
Now let us create a class which creates a new File with name make-read-only.txt file. After that just call the method
setReadOnly() method. That’s all now this file is set to only read only operations.
package com.oraclejavacertified.files.readonlywrite;
import java.io.File;
/**
* Example to set the file as read-only format.
*
* @author oraclejavacertified.com
*
*/
public class FileReadOnlyExample {
public static void main(String[] args) {
File newFile = new File("src/main/java/com/oraclejavacertified/files/readonlywrite/make-read-only.txt");
// setting the file as read only
boolean isSetToReadOnly = newFile.setReadOnly();
System.out.println("isSetToReadOnly value : "+isSetToReadOnly);
if(isSetToReadOnly) {
System.out.println("make-read-only.txt is set to read-only form");
}else {
System.out.println("Failed to set file as read only for make-read-only.txt");
}
}
}
Output:
isSetToReadOnly value : true
make-read-only.txt is set to read-only form
3. Java Example To Check File Can Be Writable
In the above section, we have made the file as read only, but let us check now wether the file is allowed for the modifications or not.
Java File API has another method canWrite() which returns true if the file writable else false that means file is read only.
Look at the below example program. We are just passing the same file name to the File class and directly checking with
canWrite() method.
After that created a new file and checked the canWrite() on the new file object.
Observe the outputs for better understanding.
package com.oraclejavacertified.files.readonlywrite;
import java.io.File;
/**
* Example to check the file is writable or not.
*
* @author oraclejavacertified.com
*
*/
public class FileCanWriteExample {
public static void main(String[] args) {
File newFile = new File("src/main/java/com/oraclejavacertified/files/readonlywrite/make-read-only.txt");
// checking the is allowed for modifications.
boolean isSetToReadOnly = newFile.canWrite();
System.out.println("Can write the file ? : " + isSetToReadOnly);
File breandNewFile = new File("src/main/java/com/oraclejavacertified/files/readonlywrite/make-new-file.txt");
// checking the is allowed for modifications.
isSetToReadOnly = breandNewFile.canWrite();
System.out.println("Can write the breandNewFile file ? : " + isSetToReadOnly);
}
}
Output:
Can write the file ? : false
Can write the breandNewFile file ? : true
4. Java Example To Make Writable from Read Only Form
Next, let us use the same read-only file and try to change its property to writable using setWritable(boolean).If true is passed then file becomes writable
If false is passed then file becomes only readable
Example program is shown below.
This method is very useful when you are working with the unix platform and we can change the files permissions easily from programming.
package com.oraclejavacertified.files.readonlywrite;
import java.io.File;
/**
* Example to convert the file from read only to writable form.
*
* @author oraclejavacertified.com
*
*/
public class FileSetWritableExample {
public static void main(String[] args) {
File newFile = new File("src/main/java/com/oraclejavacertified/files/readonlywrite/make-read-only.txt");
// Changing the file from read only to writable format.
boolean isWritableNow = newFile.setWritable(true);
System.out.println("Can write the file ? : " + isWritableNow);
}
}
Output:
Can write the file ? : true
0 comments:
Post a Comment