Java - FilePermission getActions() method



Description

The Java FilePermission getActions() method is used to retrieve the permission actions associated with a given FilePermission object. These actions can include "read", "write", "execute", and "delete".

Declaration

Following is the declaration for java.io.FilePermission.getActions() method −

public boolean getActions()

Parameters

NA

Return Value

This method returns the canonical string representation of the actions.

Exception

NA

Example - Usage of FilePermission getActions() method

The following example shows the usage of Java FilePermission getActions() method.

FilePermissionDemo.java

package com.tutorialspoint;

import java.io.FilePermission;
import java.io.IOException;

public class FilePermissionDemo {
   public static void main(String[] args) throws IOException {
      FilePermission fp = null;
      
      try {
         fp = new FilePermission("test.txt", "read");
         
         // the canonical string representation of the action
         String s = fp.getActions();
         
         // prints
         System.out.print("Action: "+s);
         
      } catch(Exception ex) {
         // if an error occurs
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Action: read

Example - Retrieving Read and Write Permissions

The following example shows the usage of Java FilePermission getActions() method.

FilePermissionDemo.java

package com.tutorialspoint;

import java.io.FilePermission;

public class FilePermissionDemo {
   public static void main(String[] args) {
      // Creating a FilePermission object with "read" and "write" actions
      FilePermission filePermission = new FilePermission("example.txt", "read,write");

      // Getting and displaying the actions associated with this permission
      String actions = filePermission.getActions();
      System.out.println("Allowed Actions: " + actions);
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Allowed Actions: read,write

Explanation

  • Creates a FilePermission object for "example.txt" with "read" and "write" permissions.

  • Calls getActions() to retrieve the assigned actions.

Example - Retrieving Execute and Delete Permissions

The following example shows the usage of Java FilePermission getActions() method.

FilePermissionDemo.java

package com.tutorialspoint;

import java.io.FilePermission;

public class FilePermissionDemo {
   public static void main(String[] args) {
      // Creating a FilePermission object with "execute" and "delete" actions
      FilePermission filePermission = new FilePermission("C:/users/example.bat", "execute,delete");

      // Getting and displaying the actions associated with this permission
      String actions = filePermission.getActions();
      System.out.println("Allowed Actions: " + actions);
   }
}

Output()

Let us compile and run the above program, this will produce the following result−

Allowed Actions: execute,delete

Explanation

  • Creates a FilePermission object for "C:/users/example.bat" with "execute" and "delete" permissions.

  • Calls getActions() to retrieve the assigned actions.

java_io_filepermission.htm
Advertisements