Java.io.File.setReadable() 方法
描述
java.io.File.setReadable(boolean readable) 方法设置此抽象路径名的访问权限。
声明
以下是 java.io.File.setReadable(boolean readable) 方法的声明 −
public boolean setReadable(boolean readable)
参数
readable − true 设置访问权限以允许读取操作。
返回值
如果操作成功,此方法返回 true,否则返回 false。
异常
SecurityException − 如果安全管理器存在并且其方法拒绝对旧路径名或新路径名的写访问。
示例
下面的例子展示了 java.io.File.setReadable(boolean readable) 方法的使用。
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();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
setReadable() succeeded?: true Is file readable?: true

