FilePermission FilePermission(String, String) in Java

FilePermission(String, String): This method is available in the java.io.FilePermission class of Java.

Syntax:

java.io.FilePermission.FilePermission(String path, String actions)

This method takes two arguments. This method creates a new FilePermission object with the specified actions.

The path is the pathname of a file or directory, and actions contain a comma-separated list of the desired actions granted on the file or directory.

Note:

1. Possible actions are"read", "write", "execute", "delete", and "readlink".

2. A pathname containing an empty string represents an empty path.

Parameters: Two parameters are required for this method.

path: the pathname of the file/directory.

actions: the action string.

Throws:

IllegalArgumentException - if actions are null, empty, malformed, or contain an action other than the specified possible actions.

Approach 1: When no exception

Java

import java.io.FilePermission;

public class FilePermissionFilePermission {
    public static void main(String[] args) {

        String path = "D:\\hello.txt";
        String actions = "read";
        FilePermission filePermission =
new FilePermission(path, actions);

        System.out.println(filePermission);
    }
}

Output:

("java.io.FilePermission" "D:\hello.txt" "read")


Approach 2: IllegalArgumentException 

Java

import java.io.FilePermission;

public class FilePermissionFilePermission {
    public static void main(String[] args) {

        String path = "D:\\hello.txt";
        String actions = null;
        FilePermission filePermission =
new FilePermission(path, actions);

        System.out.println(filePermission);
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: invalid actions mask at java.base/java.io.FilePermission.init(FilePermission.java:316) at java.base/java.io.FilePermission.<init>(FilePermission.java:489)


Some other methods of FilePermission

equals(Object)This method checks two FilePermission objects for equality. Checks that obj is a FilePermission, and has the same pathname and actions as this object.

FilePermission(String, String)This method creates a new FilePermission object with the specified actions.

getActions()This method returns the "canonical string representation" of the actions.

hashCode()This method returns the hash code value for this object.

implies(Permission)This method checks if this FilePermission object "implies" the specified permission.

newPermissionCollection()This method returns a new PermissionCollection object for storing FilePermission objects.

No comments:

Post a Comment