Java - FilePermission Class



Introduction

The Java FilePermission class represents access to a file or directory.It consists of a pathname and a set of actions valid for that pathname. Following are the important points about FilePermission −

  • The actions to be granted are passed to the constructor in a string containing a list of one or more comma-separated keywords. The possible keywords are "read", "write", "execute", and "delete".

  • Code can always read a file from the same directory it's in (or a subdirectory of that directory); it does not need explicit permission to do so.

Class declaration

Following is the declaration for Java.io.FilePermission class −

public final class FilePermission
   extends Permission
      implements Serializable

Class constructors

Sr.No. Constructor & Description
1

FilePermission(String path, String actions)

This creates a new FilePermission object with the specified actions.

Class methods

Sr.No. Method & Description
1 boolean equals(Object obj)

This method checks two FilePermission objects for equality.

2 String getActions()

This method returns the "canonical string representation" of the actions.

3 int hashCode()

This method returns the hash code value for this object.

4 boolean implies(Permission p)

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

5 PermissionCollection newPermissionCollection()

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

Methods inherited

This class inherits methods from the following classes −

  • Java.io.Permission
  • Java.io.Object

Example - Comparing Two Identical FilePermission Objects

The following example shows the usage of Java FilePermission equals(Object obj) method.

FilePermissionDemo.java

package com.tutorialspoint;

import java.io.FilePermission;

public class FilePermissionDemo {
    public static void main(String[] args) {
       // Creating two identical FilePermission objects
      FilePermission fp1 = new FilePermission("C:\\Users\\Documents\\file.txt", "read,write");
      FilePermission fp2 = new FilePermission("C:\\Users\\Documents\\file.txt", "read,write");

      // Checking equality
      if (fp1.equals(fp2)) {
         System.out.println("Both FilePermission objects are equal.");
      } else {
         System.out.println("FilePermission objects are not equal.");
      }
   }
}

Output

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

Both FilePermission objects are equal.

Explanation

  • Two FilePermission objects (fp1 and fp2) are created with the same file path and same actions (read,write).

  • The equals() method is used to compare them.

  • Since both objects are identical, it prints: Both FilePermission objects are equal.

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 - Usage of FilePermission implies(Permission p) method

The following example shows the usage of Java FilePermission implies(Permission p) 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;
      FilePermission fp1 = null;
      FilePermission fp2 = null;
      FilePermission fp3 = null;
      boolean bool = false;
      
      try {
         // create new file permissions
         fp = new FilePermission("test.txt", "read");
         fp1 = new FilePermission("test.txt", "write");
         fp2 = new FilePermission("test1.txt", "read");
         fp3 = new FilePermission("test.txt", "read");
         
         // tests if implied by this object
         bool = fp.implies(fp1);
         
         // print
         System.out.println(bool);
         
         bool = fp.implies(fp2);
         System.out.println(bool);
         
         bool = fp.implies(fp3);
         System.out.print(bool);
         
      } catch(Exception ex) {
         // if an error occurs
         ex.printStackTrace();
      }
   }
}

Output

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

false
false
true
Advertisements