How to make a file read-only or write in Java? | Java code geeks


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

1. Overview

In this article we will learn how to create readonly file in java. After creating the file in java, we need to set the property of the readOnly flag file to true. But, we cannot set this flag to true directly.

File api has a utility method setReadOnly () method that returns a Boolean value. True is returned if the file is correctly changed to read-only, otherwise false is returned.

In the last section of this article, we’ll learn how to make the format read-only.

Example to convert a writable file to read-only and vice-versa.

2. Java example to set the file to read-only

Now let’s create a class that creates a new file with the filename make-read-only.txt. After that just call the method
setReadOnly () method. That’s all now file is set to read-only operations.

01

02

03

04

05

06

07

08

09

ten

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");

        

        

        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");

        }

        

    }

}

Exit:

1

2

isSetToReadOnly value : true

make-read-only.txt is set to read-only form

3. Java example to verify that the file can be writable

In the section above, we made the file read-only, but now let’s check if the file is allowed for edits or not.

Java File API has another canWrite () method which returns true if the file is writable otherwise false, which means the file is read-only.

Take a look at the sample program below. We just pass the same filename to the File class and check directly with
can write () method.

After that created a new file and checked the can write () on the new file object.

Observe the exits for a better understanding.

01

02

03

04

05

06

07

08

09

ten

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");

        

        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");

        

        isSetToReadOnly = breandNewFile.canWrite();

        System.out.println("Can write the breandNewFile file ? : " + isSetToReadOnly);

    }

}

Exit:

1

2

Can write the file ? : false

Can write the breandNewFile file ? : true

4. Java example to make writable from a read-only form

Then let’s use the same readonly file and try to change its property to writable using setWritable (boolean).

Yes true is passed then file becomes writable

So wrong is passed then file only becomes readable

An example program is shown below.

This method is very useful when working with the unix platform and we can easily change file permissions programming.

01

02

03

04

05

06

07

08

09

ten

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");

        

        boolean isWritableNow = newFile.setWritable(true);

        System.out.println("Can write the file ? : " + isWritableNow);

    }

}

Exit:

1

Can write the file ? : true

5. Conclusion

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

GitHub

Sample Java Files

java File setReadOnly ()



Source link

Deixe uma resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *