Home »
Java programming language
Java File Class boolean setReadable(boolean read_file) method with Example
Java File Class boolean setReadable(boolean read_file) method: Here, we are going to learn about the boolean setReadable(boolean read_file) method of File class with its syntax and example.
Submitted by Preeti Jain, on July 07, 2019
File Class boolean setReadable(boolean read_file)
- This method is available in package java.io.File.setReadable( boolean read_file).
- This method is used to set the read access permission to owner’s or everybody's to read the file.
- The return type of this method is boolean so the expected value will be true or false if returns true that means file is readable else file is not readable.
- This method may raise an exception( i.e. Security Exception) if the file written permission is not given to either old or new file.
- This method is overridable and first overridable method accepts one parameter and other accepts two-parameter.
Syntax:
boolean setReadable(boolean read_file){
}
Parameter(s):
We pass only one object as a parameter in the method of the File i.e read_file(Read file).
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\\java.txt");
// By using setReadable(true) is allowed to read the
// file because we passed true as a parameter in the method.
if (file1.setReadable(true))
System.out.println("This file " + file1.getName() + " " + "is readable because read access permission is given");
else
System.out.println("This file " + file1.getName() + " " + "is not readable because read access permission is not given");
// By using setReadable(false) is not allowed to read
// the file because we passed false as a parameter in the method of the file.
if (file2.setReadable(false))
System.out.println("This file " + file2.getName() + " " + "is readable because read access permission is given");
else
System.out.println("This file " + file2.getName() + " " + "is not readable because read access permission is not given");
} catch (Exception e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Output
D:\Programs>javac ReadableFileClass.java
D:\Programs>java ReadableFileClass
This file myjava.txt is readable because read access permission is given
This file java.txt is not readable because read access permission is not given