文件類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