Java.io - SPLessons

Java.io FilePermission

Chapter 17

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io FilePermission

Java.io FilePermission

shape Introduction

Java.io FilePermission is the class used to process a file or directory. It consists set of actions and pathname valid for it. Here pathname represents pathname of the file or directory to do set of actions on it.The actions in Java.io FilePermission object to be granted then passed to constructor in the form of string containing a list of keywords like “read”, “write”, “execute”, and “delete”.
It does not required permission explicitly to do so.

Class Declaration

shape Declaration

Java.io FilePermission class is declared as :

Public final class Filepermission extends permission implements Serializable

Class Constructors

shape Table

Constructor Description
FilePermission(String path, String actions) This constructor creates a new FilePermission object with the specified actions.

Class Methods

shape Table

Method Description
boolean equals(Object obj) For equality this method checks two FilepPermission objects.
int hashCode() This method returns the hash code value for FilePermission object.
String getActions() The “canonical string representation” of the actions returned by this method.
boolean implies(Permission p) This method tests that the FilePermission object “implies” the specified permission ‘p’.
PermissionCollection newPermissionCollection() For storing FilePermission objects This method returns a new PermissionCollection object.

Inherited Methods

shape Description

From the following classes, methods are inherited to Java.io.FilePermission

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

shape Examples

Usage of string getActions() method.

import java.io.FilePermission;
import java.io.IOException;
public class FilePermission 
{
   public static void main(String[] args) throws IOException 
   {
      
      FilePermission fp = null;
      
      try{
         // creating file permissions object
         fp=new FilePermission("C://test.txt", "read");
         
         // The canonical string representation of the action
         String s = fp.getActions();
         
         // print action
         System.out.print("Action: "+s);
         
      }catch(Exception ex)
      {
         // if any error occurs
         ex.printStackTrace();
      }
   }
}

Output
The result will be as follows.

Action: read

Usage of boolean equals(object obj) method.

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

public class FilePermission 
{
   public static void main(String[] args) throws IOException 
   {
      
      FilePermission fp = null;
      FilePermission fp1 = null;
      FilePermission fp2 = null;
      FilePermission fp3 = null;
      boolean bl = false;
      
      try{
         // creating file permissions
         fp=new FilePermission("C://test.txt", "read");
         fp1=new FilePermission("C://test1.txt", "read");
         fp2=new FilePermission("C://test.txt", "write");
         fp3=new FilePermission("C://test.txt", "read");
         
         // checks for equality
         bl = fp.equals(fp1);

         // print
         System.out.println(bl);
         
         // checks for equality
         bl = fp.equals(fp2);
         
         // print
         System.out.println(bl);
         
         // checks for equality
         bl = fp.equals(fp3);
         
         // print
         System.out.print(bl);
      }catch(Exception ex){
         // if an error occurs
         ex.printStackTrace();
      }finally{
         
      }
   }
}

Output
The result will be as follows.

false
false
true

Summary

shape Key Points

  • Java.io FilePermission is the class used to process a file or directory.
  • It consists set of actions and pathname valid for it. Here pathname represents pathname of the file or directory to do set of actions on it.
  • The actions in File permission object to be granted then passed to constructor in the form of string containing a list of keywords like “read”, “write”, “execute”, and “delete”.