Make file Read Only in Java

Price: INR 8,999.00
Price: INR 15,999.00
Was: INR 16,999.00

To make file Read Only in Java, we use setReadOnly() method of the java.io.File 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