Syntax:
句法:
public void checkWrite(FileDescriptor file_des);
public void checkWrite(String fi);
SecurityManager类的checkWrite()方法 (SecurityManager Class checkWrite() method)
checkWrite() method is available in java.lang package.
checkWrite()方法在java.lang包中可用。
checkWrite(FileDescriptor file_des) method invokes checkPermission with the RuntimePermission("writeFileDescriptor") to write the given file descriptor.
checkWrite(FileDescriptor file_des)方法使用RuntimePermission(“ writeFileDescriptor”)调用checkPermission来写入给定的文件描述符。
checkWrite(String fi) method invokes checkPermission with the FilePermission(fi,"write") to write to the file by the given fi parameter.
checkWrite(String fi)方法通过给定的fi参数调用带有FilePermission(fi,“ write”)的checkPermission来写入文件。
checkWrite(FileDescriptor file_des), checkWrite(String fi) methods may throw an exception at the time of writing the file from different media.
从不同媒体写入文件时, checkWrite(FileDescriptor file_des) , checkWrite(String fi)方法可能会引发异常。
checkWrite(FileDescriptor file_des):
checkWrite(FileDescriptor file_des):
- SecurityException – This exception may throw when the calling thread is not allowed to write the given file descriptor.
- NullPointerException – This exception may throw when the given parameter is null.
checkWrite(String fi):
checkWrite(String fi):
- SecurityException – This exception may throw when the calling thread is not allowed to write the given file.
- NullPointerException – This exception may throw when the given parameter is null.
These are non-static methods, it is accessible with the class object only and, if we try to access these methods with the class name then we will get an error.
这些是非静态方法,只能通过类对象访问,如果尝试使用类名称访问这些方法,则会收到错误消息。
Parameter(s):
参数:
In the first case, FileDescriptor file_des - This parameter represents the system-specific file descriptor.
在第一种情况下, FileDescriptor file_des-此参数表示系统特定的文件描述符。
In the second case, String fi - This parameter represents the system-specific file name.
在第二种情况下, 字符串fi-此参数表示系统特定的文件名。
Return value:
返回值:
The return type of this method is void, it returns nothing.
此方法的返回类型为void ,不返回任何内容。
Example:
例:
// Java program to demonstrate the example
// of checkWrite() method of SecurityManager class
import java.security.*;
import java.io.*;
public class CheckWrite extends SecurityManager {
public static void main(String[] args) {
FileDescriptor file_desc = new FileDescriptor();
String fi = "getProperties().doc";
// By using setProperty() method is to set the policy property
// with security manager
System.setProperty("java.security.policy", "file:/C:/java.policy");
// Instantiating a CheckWrite object
CheckWrite cw = new CheckWrite();
// By using setSecurityManager() method is to set the
// security manager
System.setSecurityManager(cw);
// By using CheckWrite(FileDescriptor) method is to
// check that write file by using file descriptor
cw.checkWrite(file_desc);
// By using CheckWrite(String) method is to
// check that write file by using String
cw.checkWrite(fi);
// Display the message
System.out.println("Accepted..");
}
}
Output
输出量
Exception in thread "main" java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "writeFileDescriptor")
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.checkWrite(SecurityManager.java:727)
at CheckWrite.main(CheckWrite.java:25)
翻译自: https://www.includehelp.com/java/securitymanager-checkwrite-method-with-example.aspx