Java.io.File.canRead() 方法
描述
如果可以读取文件,则 java.io.File.canRead() 方法返回 true,并用其抽象名称表示。
声明
以下是 java.io.File.canRead() 方法的声明 −
public boolean canRead()
参数
NA
返回值
此方法返回布尔值。 是的,如果指定的文件存在其路径名并且允许应用程序读取该文件。
异常
SecurityException − 如果 SecurityManager.checkRead(java.lang.String) 方法拒绝应用程序的读取访问。
示例
下面的例子展示了 java.io.File.canRead() 方法的使用。
package com.tutorialspoint;
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File f = null;
try {
// create new file
f = new File("c:/test.txt");
// returns true if the file can be read
boolean bool = f.canRead();
// print
System.out.print("File can be read: "+bool);
} catch(Exception e) {
// if any I/O error occurs
e.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
File can be read: false