SecurityManager類checkDelete()方法
- checkDelete() 方法在 java.lang 包中可用。
- checkDelete() 方法使用 FilePermission(filename,"delete") 調用 checkPermission。
- checkDelete() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
- checkDelete() 方法可能會在刪除文件時拋出異常。
- SecurityException - 當調用線程不允許刪除文件並且通過使用 File 類的 delete() 方法為當前安全管理器調用時,可能會拋出此異常。
NullPointerException - 當給定參數為空時,可能會拋出此異常。
用法:
public void checkDelete(String file_name);
參數:
String file_name
- 代表 system-dependent 文件名。
返回值:
這個方法的返回類型是void
,它什麽都不返回。
例:
// Java program to demonstrate the example
// of void checkDelete(String file_name)
// method of SecurityManager
public class CheckDelete {
public static void main(String[] args) {
// By using setProperty() method is to set the policy property
// with security manager
System.setProperty("java.security.policy", "file:/C:/java.policy");
// Instantiating a SecurityManager object
SecurityManager smgr = new SecurityManager();
// By using setSecurityManager() method is to set the
// security manager
System.setSecurityManager(smgr);
// By using checkDelete(String file_name) method is to check
//that file is deleted or not
smgr.checkDelete("getProperties().doc");
// Display the message when file is deleted
System.out.println("File Deleted Successfuly..");
}
}
輸出
Exception in thread "main" java.security.AccessControlException:access denied ("java.io.FilePermission" "getProperties().doc" "delete") at java.base/java.security.AccessControlContext.checkPermission(AccessControlContext.java:472) at java.base/java.security.AccessController.checkPermission(AccessController.java:897) at java.base/java.lang.SecurityManager.checkPermission(SecurityManager.java:322) at java.base/java.lang.SecurityManager.checkDelete(SecurityManager.java:780) at CheckDelete.main(CheckDelete.java:20)
相關用法
- Java SecurityManager checkPackageAccess()用法及代碼示例
- Java SecurityManager checkPrintJobAccess()用法及代碼示例
- Java SecurityManager checkSystemClipboardAccess()用法及代碼示例
- Java SecurityManager checkPropertiesAccess()用法及代碼示例
- Java SecurityManager checkLink()用法及代碼示例
- Java SecurityManager checkWrite()用法及代碼示例
- Java SecurityManager checkAwtEventQueueAccess()用法及代碼示例
- Java SecurityManager checkPropertyAccess()用法及代碼示例
- Java SecurityManager checkSetFactory()用法及代碼示例
- Java SecurityManager checkSecurityAccess()用法及代碼示例
- Java SecurityManager checkPermission()用法及代碼示例
- Java SecurityManager checkRead()用法及代碼示例
- Java SecurityManager checkAccept()用法及代碼示例
- Java SecurityManager checkTopLevelWindow()用法及代碼示例
- Java SecurityManager checkAccess()用法及代碼示例
- Java SecurityManager checkConnect()用法及代碼示例
- Java SecurityManager checkMemberAccess()用法及代碼示例
- Java SecurityManager checkListen()用法及代碼示例
- Java SecurityManager checkMulticast()用法及代碼示例
- Java SecurityManager checkExec()用法及代碼示例
注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java SecurityManager checkDelete() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。