Core Java

How To Make A File Read Only Or Writable In Java?

A quick guide on how to make a file read only in java using setReadOnly() method from File API.

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.

In the last section of this article, we will learn how to make the writable from read only format.

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.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.javaprogramto.files.readonlywrite;
 
import java.io.File;
 
/**
 * Example to set the file as read-only format.
 *
 * @author javaprogramto.com
 *
 */
public class FileReadOnlyExample {
 
    public static void main(String[] args) {
 
        File newFile = new File("src/main/java/com/javaprogramto/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:

1
2
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.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.javaprogramto.files.readonlywrite;
 
import java.io.File;
 
/**
 * Example to check the file is writable or not.
 *
 * @author javaprogramto.com
 *
 */
public class FileCanWriteExample {
 
    public static void main(String[] args) {
 
        File newFile = new File("src/main/java/com/javaprogramto/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/javaprogramto/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:

1
2
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.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.javaprogramto.files.readonlywrite;
 
import java.io.File;
 
/**
 * Example to convert the file from read only to writable form.
 *
 * @author javaprogramto.com
 *
 */
public class FileSetWritableExample {
 
    public static void main(String[] args) {
 
        File newFile = new File("src/main/java/com/javaprogramto/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:

1
Can write the file ? : true

5. Conclusion

In this article, we’ve seen how to change file permissions from read only to writable and writable to read only in java with examples.

GitHub

Java Files Examples

java File setReadOnly()

Published on Java Code Geeks with permission by Venkatesh Nukala, partner at our JCG program. See the original article here: How To Make A File Read Only Or Writable In Java?

Opinions expressed by Java Code Geeks contributors are their own.

Venkatesh Nukala

Venkatesh Nukala is a Software Engineer working for Online Payments Industry Leading company. In my free time, I would love to spend time with family and write articles on technical blogs. More on JavaProgramTo.com
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button