Java - File setReadOnly() method



Description

The Java File setReadOnly() method is used to make a file read-only, meaning it cannot be modified or deleted. It returns true if the operation is successful and false if it fails (e.g., due to permissions or file system restrictions).

Declaration

Following is the declaration for java.io.File.setReadOnly() method −

public boolean setReadOnly()

Parameters

readable− true sets the access permission to allow read operations, false denies read operation.

Return Value

This method returns true if the operation succeeded, else false.

Exception

  • SecurityException − If a security manager exists and its method denies write access to the pathnames.

Example - Usage of File setReadOnly() method

The following example shows the usage of Java File setReadOnly() method.

FileDemo.java

package com.tutorialspoint;

import java.io.File;

public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      boolean bool = false;
      
      try {  
      
         // create new File object
         f = new File("C:/test.txt");
         
         // returns true if file exists
         bool = f.exists();
         
         // if file exists
         if(bool) {
         
            // set file as read only
            bool = f.setReadOnly();
            
            // print
            System.out.println("setReadonly() succeeded?: "+bool);
            
            // checks whether the file is writable
            bool  = f.canWrite();
            
            // prints
            System.out.print("Is file writable?: "+bool);
         }
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

setReadonly() succeeded?: true
Is file writable?: false

Example - Making a File Read-Only

The following example shows the usage of Java File setReadOnly() method.

FileDemo.java

package com.tutorialspoint;

import java.io.File;

public class FileDemo {
   public static void main(String[] args) {
      // Create a File object representing an existing file
      File file = new File("example.txt");

      // Check if the file exists
      if (file.exists()) {
         // Set the file to read-only
         if (file.setReadOnly()) {
            System.out.println("File is now read-only: " + file.getAbsolutePath());
         } else {
            System.out.println("Failed to set the file as read-only.");
         }
      } else {
         System.out.println("File does not exist.");
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

File is now read-only: C:\Users\YourName\example.txt

Explanation

  • The program creates a File object for "example.txt".

  • It checks if the file exists before modifying its properties.

  • It calls setReadOnly() to prevent modifications to the file.

  • If successful, it prints a confirmation message.

  • If it fails, an error message is displayed.

Example - Checking If a File is Read-Only After Setting It

The following example shows the usage of Java File setReadOnly() method.

FileDemo.java

package com.tutorialspoint;

import java.io.File;

public class FileDemo {
   public static void main(String[] args) {      
      // Create a File object representing an existing file
      File file = new File("example.txt");

      // Check if the file exists
      if (file.exists()) {
         // Set the file to read-only
         if (file.setReadOnly()) {
            System.out.println("File has been set to read-only.");

            // Check if the file is writable
            if (!file.canWrite()) {
               System.out.println("Confirmed: File is now read-only.");
            } else {
               System.out.println("Warning: File is still writable.");
            }
         } else {
            System.out.println("Failed to set file as read-only.");
         }
      } else {
         System.out.println("File does not exist.");
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

File has been set to read-only.
Confirmed: File is now read-only.

Explanation

  • The program sets the file as read-only using setReadOnly().

  • It then verifies if the file is read-only by checking file.canWrite().

  • If file.canWrite() returns false, the file is confirmed as read-only.

  • If it still returns true, the system did not apply the read-only property.

java_io_file_methods.htm
Advertisements