How to make a file read only in java

In this post, we will see how to make a read only file in java. It is very simple. You need to just call java.io.File ‘s  setReadOnly() method.

1) How to make a file read only

Java program:

When you run above program, you will get following output:

2) How to make it writable  again

If you have made file read only, you can make file writable again by using method setWritable(true).
This method is introduced in java 1.6.


Java program:

When you run above program, you will get following output:
 

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *