Home » Java programming language

Java File Class boolean setReadable(boolean read_file , boolean owner_read) method with Example

Java File Class boolean setReadable(boolean read_file , boolean owner_read) method: Here, we are going to learn about the boolean setReadable(boolean read_file , boolean owner_read) method of File class with its syntax and example.
Submitted by Preeti Jain, on July 07, 2019

File Class boolean setReadable(boolean read_file , boolean owner_read)

  • This method is available in package java.io.File.setReadable(boolean read_file, boolean owner_read).
  • This method is used to set the read access permission to owner's or everybody's to read the file.
  • This method accepts two parameters the first parameter is for file access and another parameter is for file is readable by owner only or everybody's (i.e. If we pass true as a first parameter that means file is readable else false file is not readable and If we pass true as a second parameter that means file is readable by owner's only else false that's means file is readable for everybody's) .
  • This method may raise an exception( i.e. Security Exception) if the file written permission is not given.

Syntax:

    boolean setReadable(Boolean read_file , boolean owner_read){
    }

Parameter(s):

We pass two objects as a parameter in the method of the File i.e read_file(Read file) and owner_read(File is readable by the owner or everybody's).

Return value:

The return type of this method is Boolean if it returns true so the file is readable and else returns false file is not readable.

Java program to demonstrate example of setReadable() method

// import the File class because we will use File class methods
import java.io.File;

// import the Exception class because it may raise 
// an exception when working with files
import java.lang.Exception;

public class ReadableFileClass {
    public static void main(String[] args) {
        try {
            // Specify the path of file and we use double slashes 
            // to escape '\' character sequence for windows otherwise 
            // it will be considerable as url.
            File file1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt");
            File file2 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava1.txt");

            // By using setReadable(true , true)  is allowed to read 
            // the file by the owner only [i.e. First true is for 
            // whether file is readable or not (i.e. true means it is readable) 
            // and Second true is for whether file is readable by owner or 
            // everbody's (i.e. true means file is readable by owner only) ]
            if (file1.setReadable(true, true))
                System.out.println("This file " + file1.getName() + " " + "is readable  by the owner only");
            else
                System.out.println("This file " + file1.getName() + " " + "is not readable ");

            // By using setReadable(true , false)  is allowed 
            // to read the file by the everbody's 
            // [i.e. First true is for whether file is readable or not 
            // (i.e. true means it is readable) and Second true 
            // is for whether file is readable by owner or everybody's 
            // (i.e. false means file is readable by everybody's) ]
            if (file2.setReadable(true, false))
                System.out.println("This file " + file2.getName() + " " + "is readable by everybody's");
            else
                System.out.println("This file " + file2.getName() + " " + "is not readable");
        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

D:\Programs>javac ReadableFileClass.java

D:\Programs>java Java8 ReadableFileClass
This file myjava.txt is readable  by the owner only
This file myjava1.txt is readable by everybody's





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.