Open In App

File setReadable() function in Java with Examples

The setReadable() method is a part of File class. The function sets the owner’s or everybody’s permission to read the abstract pathname.
The function is an overloaded function One function requires two parameters and the other only one.

Function Signature:



public boolean setReadable(boolean a, boolean b)
public boolean setReadable(boolean a)

Function Syntax:

file.setReadable(boolean a, boolean b)
file.setReadable(boolean a)

Parameters: The function is a overloaded function:



For the second overload:

  • If a true value is passed as a parameter then it is allowed to read the abstract pathname, else it is not allowed to read the file.
  • Return value: This function returns boolean value, whether the operation succeeded or not.

    Exception: This method throws Security Exception if the function is not allowed write access to the file.

    Below programs will illustrate the use of the setReadable() function

    Example 1: We will try to change the setReadable permission of the owner of an existing file in f: directory.




    // Java program to demonstrate the
    // use of setReadable() function
      
    import java.io.*;
      
    public class solution {
        public static void main(String args[])
        {
      
            // try-catch block to handle exceptions
            try {
      
                // Create a file object
                File f = new File("f:\\program.txt");
      
                // Check if the Readable permission
                // can be set to new value
                if (f.setReadable(true)) {
      
                    // Display that the Readable permission
                    // is be set to new value
                    System.out.println("Readable permission"
                                       + " is set");
                }
                else {
      
                    // Display that the Readable permission
                    // cannot be set to new value
                    System.out.println("Readable permission"
                                       + " cannot be set");
                }
            }
            catch (Exception e) {
                System.err.println(e.getMessage());
            }
        }
    }
    
    

    Output:

    Readable permission is set
    

    Example 2: We will try to change the setReadable permission of every one of an existing file in f: directory.




    // Java program to demonstrate the
    // use of setReadable() function
      
    import java.io.*;
      
    public class solution {
        public static void main(String args[])
        {
      
            // try-catch block to handle exceptions
            try {
      
                // Create a file object
                File f = new File("f:\\program.txt");
      
                // Check if the Readable permission
                // can be set to new value
                if (f.setReadable(true, false)) {
      
                    // Display that the Readable permission
                    // is be set to new value
                    System.out.println("Readable permission"
                                       + " is set");
                }
                else {
      
                    // Display that the Readable permission
                    // cannot be set to new value
                    System.out.println("Readable permission"
                                       + " cannot be set");
                }
            }
            catch (Exception e) {
                System.err.println(e.getMessage());
            }
        }
    }
    
    

    Output:

    Readable permission is set
    

    The programs might not run in an online IDE. please use an offline IDE and set the Parent file of the file


    Article Tags :