setExecutable()方法是File类的一部分。该函数设置所有者或所有人执行抽象路径名的权限。
该函数是重载函数一个函数需要两个参数,而另一个仅需要一个。
函数签名:
public boolean setExecutable(boolean a, boolean b)
public boolean setExecutable(boolean a)
函数语法:
file.setExecutable(boolean a, boolean b)
file.setExecutable(boolean a)
参数:该函数是重载函数:
对于第一次过载:
如果将真值作为第一个参数传递,则允许它执行抽象路径名,否则不允许执行该文件。
如果将true值作为第二个参数传递,则执行权限仅适用于所有者,否则适用于所有人
对于第二次过载:
如果将真实值作为参数传递,则允许它执行抽象路径名,否则不允许执行该文件。
返回值:无论操作是否成功,此函数都会返回布尔值。
异常:如果不允许该函数对该文件进行写访问,则此方法将引发Security Exception。
下面的程序将说明setExecutable()函数的用法
范例1:我们将尝试更改f:目录中现有文件的所有者的setExecutable权限。
// Java program to demonstrate the
// use of setExecutable() function
import java.io.*;
public class solution {
public static void main(String args[])
{
// try-catch block to handle exceptions
try {
// Create a file object
File f = new File("f:\\program.txt");
// Check if the Executable permission
// can be set to new value
if (f.setExecutable(true)) {
// Display that the Executable permission
// is be set to new value
System.out.println("Executable permission"
+ " is set");
}
else {
// Display that the Executable permission
// cannot be set to new value
System.out.println("Executable permission"
+ " cannot be set");
}
}
catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
输出:
Executable permission is set
范例2:我们将尝试更改f:目录中每个现有文件的setExecutable权限。
// Java program to demonstrate the
// use of setExecutable() function
import java.io.*;
public class solution {
public static void main(String args[])
{
// try-catch block to handle exceptions
try {
// Create a file object
File f = new File("f:\\program.txt");
// Check if the Executable permission
// can be set to new value
if (f.setExecutable(true, false)) {
// Display that the Executable permission
// is be set to new value
System.out.println("Executable permission"
+ " is set");
}
else {
// Display that the Executable permission
// cannot be set to new value
System.out.println("Executable permission"
+ " cannot be set");
}
}
catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
输出:
Executable permission is set
这些程序可能无法在在线IDE中运行。请使用离线IDE并设置文件的父文件