Make file Read Only in Java

To make file Read Only in Java, we use setReadOnly() method of the package. Since JDK 1.6, a new setWritable() method is provided to make a file be writable again.

We can also check whether the file is writable or read-only using the canWrite() method. We will see the whole process in the below-mentioned example. We will see, how can we make the file read-only , how to check whether the file is writable or read-only.


Make File Read Only in Java with Example

Let’s understand this with an easy example. In this tutorial, First, we will make a file read-only then we will verify whether the file is read-only or writable and later on we will make it writable.


The above code first method makeFileReadonly() will make the file SampleWriterFile.txt read-only.

Second method verifyFile() will verify, Is SampleWriterFile.txt read-only for java or writable.

The last and third method makeFileWritable() will again make the file writable.

You can refer the above code and test it on your local machine.

0 Comment

Leave a Reply