Java Program to Make a File Read-Only

Read-Only is the file attribute that the operating system assigns to the file. When the file is flagged as read-only, it means that it can only be opened or read, one cannot change the name of the file, can not rewrite or append the content of the file, and also cannot delete the file.

Method 1:

Using setLastModified Method

To make the file read-only, setReadOnly() method of File Class is used. This method returns the boolean value, which is used to check whether the task(to make the file read-only) got successful or not. If the value returned by the method is true, it means that the task was successful and vice versa.

Parameters: The function doesn’t require any parameters.



Return Value: The function returns boolean data type. The function returns true if the File object could be set as Read-Only else false.

Exception: This method throws SecurityException if the method does not allow to write access to the file

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to make the file as read only
 
import java.io.File;
 
public class GFG {
   
    public static void main(String[] args)
    {
        // flag variable which contains the boolean
        // value returned by setReadOnly() funtion
        boolean flag;
       
        try {
           
            File file = new File("/home/mayur/GFG.java");
           
            // creates a new file
            file.createNewFile();
           
            // flag the file to be read-only
            flag = file.setReadOnly();
           
            System.out.println("Is File is read-only ? : "
                               + flag);
           
            // checking whether Is file  writable
            flag = file.canWrite();
            System.out.println("Is File is writable ? : "
                               + flag);
        }
       
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

chevron_right


 
 

Output:

 

Java Program to Make a File Read-Only

Method 2: Using setWritable() method

Here by passing “false” as an argument to the setWritable() method, we can make a file read only.

The following code will help you understand how to make a file read only using setWritable().

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.io.File;
 
public class ChangetoReadOnly {
 
    public static void main(String[] args)
    {
        try {
            File file = new File(
                "C://Users//sai mohan pulamolu//Desktop//test.txt");
           
            // making the file to read only mode
            file.setWritable(false);
           
            // check if the  file is writable or not
            // if not writable then it is readonly file.
            if (!file.canWrite()) {
                System.out.println(
                    "This File is read only.");
            }
            else {
                System.out.println(
                    "This File is writable.");
            }
        }
        catch (Exception e) {
            System.out.println(
                "Sorry unable to change to readonly mode.");
        }
    }
}

chevron_right



Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohan1240760

Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.