文件类boolean setExecutable(boolean exec_file,boolean owner_access) (File Class boolean setExecutable(boolean exec_file , boolean owner_access))
This method is available in package java.io.File.setExecutable(boolean exec_file , boolean owner_access).
软件包java.io.File.setExecutable(boolean exec_file,boolean owner_access)中提供了此方法。
This method is used to set the access permission to owner's or everybody's to execute the file.
此方法用于将所有者或所有人的访问权限设置为执行文件。
This method accepts two parameters the first parameter is for file access and another parameter is for file is accessible by owner only or everybody's (i.e. If we pass true as a first parameter that means file is accessible else false file is not accessible and If we pass true as a second parameter that means file is accessible by owner's only else false that's means file is accessible to everybody's).
此方法接受两个参数,第一个参数用于文件访问,另一个参数用于文件访问,只有所有者或所有人都可以访问(即,如果将true作为第一个参数传递,则意味着文件可访问,否则,将无法访问错误文件;如果传递作为第二个参数,则为true,表示文件只能被所有者访问;否则为false,表示每个人都可以访问文件)。
This method may raise an exception(i.e. Security Exception) if the file written permission is not given.
如果未提供文件写入权限,则此方法可能会引发异常(即Security Exception)。
Syntax:
句法:
boolean setExecutable(Boolean exec_file , boolean owner_access){
}
Parameter(s):
参数:
We pass two objects as a parameter in the method of the File i.e exec_file(Executable file ) and owner_access( File is executable by the owner or everybody's).
我们将两个对象作为参数传递给File的方法,即exec_file(Executable file)和owner_access(文件可由所有者或所有人执行)。
Return value:
返回值:
The return type of this method is Boolean if it returns true so the file is executable and else returns false file is not executable.
如果此方法返回true,则该方法的返回类型为Boolean ,因此该文件是可执行的,否则返回false不可执行的文件。
Java程序演示setExecutable()方法的示例 (Java program to demonstrate example of setExecutable() method)
// import the File class because we will use File class methods
import java.io.File;
// import the Exception class because
// it may raise an exception when working with files
import java.lang.Exception;
public class ExecutableFileClass {
public static void main(String[] args) {
try {
// Specify the path of file and we use double slashes
// to escape ''\' character sequence for windows otherwise
// it will be considerable as url.
File file1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt");
File file2 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava1.txt");
// By using setExecutable(true , true) is allowed
// to execute the file by the owner only
// [i.e. First true is for whether file is executable or not
// (i.e. true means it is executable) and Second true is
// for whether file is accessible by owner or everbody's
// (i.e. true means file is executable by owner only) ]
if (file1.setExecutable(true, true))
System.out.println("This file " + file1.getName() + " " + "is executable by the owner only");
else
System.out.println("This file " + file1.getName() + " " + "is not executable");
// By using setExecutable(true , false) is allowed
// to execute the file by the everybody's
// [i.e. First true is for whether file is executable or not
// (i.e. true means it is executable) and Second true is
// for whether file is accessible by owner or everybody's
// (i.e. false means file is executable by everybody's) ]
if (file2.setExecutable(true, false))
System.out.println("This file " + file2.getName() + " " + "is executable by everybody's");
else
System.out.println("This file " + file2.getName() + " " + "is not executable");
} catch (Exception e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Output
输出量
D:\Programs>javac ExecutableFileClass.java
D:\Programs>java ExecutableFileClass
This file myjava.txt is executable by the owner only
This file myjava1.txt is executable by everybody's
翻译自: https://www.includehelp.com/java/file-class-boolean-setexecutable-boolean-exec_file-boolean-owner_access-method-with-example.aspx