Home »
Java programming language
Java SecurityManager checkWrite() method with example
SecurityManager Class checkWrite() method: Here, we are going to learn about the checkWrite() method of SecurityManager Class with its syntax and example.
Submitted by Preeti Jain, on December 14, 2019
Syntax:
public void checkWrite(FileDescriptor file_des);
public void checkWrite(String fi);
SecurityManager Class checkWrite() method
- checkWrite() method is available in java.lang package.
- checkWrite(FileDescriptor file_des) method invokes checkPermission with the RuntimePermission("writeFileDescriptor") to write the given file descriptor.
- checkWrite(String fi) method invokes checkPermission with the FilePermission(fi,"write") to write to the file by the given fi parameter.
- 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):
- 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):
- 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.
- In the second case, String fi - This parameter represents the system-specific file name.
Return value:
The return type of this method is void, it returns nothing.
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)