Java.io.File.canExecute() 方法
描述
如果文件可以通过其抽象名称执行,java.io.File.canExecute() 方法返回 true。
声明
以下是 java.io.File.canExecute() 方法的声明 −
public boolean canExecute()
参数
NA
返回值
此方法返回布尔值。 如果路径名存在且允许应用程序执行该文件,则为真。
异常
SecurityException − 如果文件不允许被执行。
示例
下面的例子展示了 java.io.File.canExecute() 方法的使用。
package com.tutorialspoint;
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File f = null;
String[] strs = {"test.txt", "/test.txt"};
try {
// for each string in string array
for(String s:strs ) {
// create new file
f = new File(s);
// true if the file is executable
boolean bool = f.canExecute();
// find the absolute path
String a = f.getAbsolutePath();
// prints absolute path
System.out.print(a);
// prints
System.out.println(" is executable: "+ bool);
}
} catch(Exception e) {
// if any I/O error occurs
e.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
D:\EclipseAndroid\IO\BufferedInputStream\test.txt is executable: true D:\test.txt is executable: false

