boolean canExecute()
描述 (Description)
如果文件可以通过其抽象名称执行,则java.io.File.canExecute()方法返回true。
声明 (Declaration)
以下是java.io.File.canExecute()方法的声明 -
public boolean canExecute()
参数 (Parameters)
NA
返回值 (Return Value)
此方法返回布尔值。 如果路径名存在且允许应用程序执行该文件,则为True。
异常 (Exception)
SecurityException - 如果不允许执行该文件。
例子 (Example)
以下示例显示了java.io.File.canExecute()方法的用法。
package com.iowiki;
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