Java.io.File.setReadable() Method
Advertisements
Description
The java.io.File.setReadable(boolean readable) method sets the access permission for this abstract pathname.
Declaration
Following is the declaration for java.io.File.setReadable(boolean readable) method:
public boolean setReadable(boolean readable)
Parameters
readable -- true to set the access permisson to allow 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 either the old or new pathnames.
Example
The following example shows the usage of java.io.File.setReadable(boolean readable) method.
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 read permission
bool = f.setReadable(true);
// print
System.out.println("setReadable() succeeded?: "+bool);
// checks whether the file is readable
bool = f.canRead();
// prints
System.out.print("Is file readable?: "+bool);
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result:
setReadable() succeeded?: true Is file readable?: true