java.io.FilePermission class is used to represent access to a file or a directory. These accesses are in the form of a path name and a set of actions associated to the path name(specifies which file to be open along with the extension and the path). For Example, in FilePermission(“Beginner.txt”, “read”) “Beginner.txt” is the path and “read” is action being performed. These actions are as follows :
- read : read permission to the file
- write : write permission to the file
- delete : delete permission to the file by calling File.delete
- readlink : read link permission
- execute : executed the permission
public final class FilePermission
extends Permission
implements Serializable
FilePermission(String p, String a) : Creates a new file permission object with "a" action.
Methods of FilePermission Class :
- equals(Object FP_obj) : java.io.FilePermission.equals(Object FP_obj) tells whether the two (i.e. checks the FP_obj pathname and filename with this object) FilePermission objects are equal or not.
- Syntax :
public boolean equals(Object FP_obj)
Parameters :
FP_obj : the FilePermission object to be verified with this object
Returns :
true : if both the objects are equal else, false.
Exception :
----------
Java
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
boolean bool = false ;
FilePermission FP_obj1 = new FilePermission("Beginner", "read");
FilePermission FP_obj2 = new FilePermission("ABC", "write");
FilePermission FP_obj3 = new FilePermission("Beginner", "read");
bool = FP_obj2.equals(FP_obj1);
System.out.println("Whether FP_obj1 equals FP_obj2 : " + bool);
bool = FP_obj2.equals(FP_obj3);
System.out.println("Whether FP_obj2 equals FP_obj2 : " + bool);
bool = FP_obj1.equals(FP_obj3);
System.out.println("Whether FP_obj3 equals FP_obj1 : " + bool);
}
}
|
Whether FP_obj1 equals FP_obj2 : false
Whether FP_obj2 equals FP_obj2 : false
Whether FP_obj3 equals FP_obj1 : true
- getActions() : java.io.FilePermission.getActions() tells the action of this FilePermission Object. If in case there are two actions along with the object : delete and read, then the method will return “read, delete”. In such cases this method returns “canonical string” : read, write, execute, delete, readlink
- Syntax :
public String getActions()
Parameters :
----------
Returns :
canonical string : representing the actions associated with the object.
Exception :
----------
Java
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
FilePermission FP_obj1 = new FilePermission("Beginner", "read, delete, write");
FilePermission FP_obj2 = new FilePermission("ABC", "write, read, execute");
FilePermission FP_obj3 = new FilePermission("Beginner", "delete, readlink, read");
String str = FP_obj1.getActions();
System.out.println("Actions with FP_obj1 : " + str);
str = FP_obj2.getActions();
System.out.println("Actions with FP_obj2 : " + str);
str = FP_obj3.getActions();
System.out.println("Actions with FP_obj3 : " + str);
}
}
|
Actions with FP_obj1 : read,write,delete
Actions with FP_obj2 : read,write,execute
Actions with FP_obj3 : read,delete,readlink
- hashCode() : java.io.FilePermission.hashCode() returns hash code for the argumented FilePermission Object
- Syntax :
public int hashCode()
Parameters :
--------
Returns :
hash code value for the argumented object
Exception :
----------
Java
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
FilePermission FP_obj1= new FilePermission("Beginner", "read, delete, write");
int i = FP_obj1.hashCode();
System.out.println("hashCode value for FP_obj1 : " + i);
}
}
|
hashCode value for FP_obj1 : 0
- implies(Permission arg) :java.io.FilePermission.implies(Permission arg) tells whether this FilePermision has the argumented Permission or not. Syntax :
public boolean implies(Permission arg)
Parameters :
arg : Permission to be checked
Returns :
true if the FilePermission object has the argumented Permission else, false
Exception :
----------
Java
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
FilePermission FP_obj1 = new FilePermission("Beginner", "read");
FilePermission FP_obj2 = new FilePermission("ABC", "write");
FilePermission FP_obj3 = new FilePermission("Beginner", "delete");
boolean check = FP_obj1.implies(FP_obj2);
System.out.println("Using implies() for FP_obj1 : " + check);
check = FP_obj2.implies(FP_obj2);
System.out.println("Using implies() for FP_obj2 : " + check);
}
}
|
Using implies() for FP_obj1 : false
Using implies() for FP_obj2 : true
- newPermissionCollection() :java.io.FilePermission.newPermissionCollection() creates PermissionCollection object having the FilePermission objects. Syntax :
public PermissionCollection newPermissionCollection()
Parameters :
arg : Permission to be checked
Returns :
new PermissionCollection object having the FilePermission objects.
Exception :
----------
Java
import java.io.*;
import java.security.PermissionCollection;
public class NewClass
{
public static void main(String[] args) throws IOException
{
FilePermission FP_obj1 = new FilePermission("Beginner.txt", "read");
PermissionCollection FP = FP_obj1.newPermissionCollection();
FP.add(FP_obj1);
boolean check = FP.implies( new FilePermission("Beginner.txt", "read"));
System.out.println("Is newPermissionCollection() working : " + check);
}
}
|
Is newPermissionCollection() working : true
Contact Us