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 the extension and the path).
For Example, in FilePermission(“GEEKS.txt”, “read”) “GEEKS.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
Declaration :
public final class FilePermission extends Permission implements Serializable
Constructors :
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 : ----------
Implementation :
// Java Program illustrating equals() method
import
java.io.*;
public
class
NewClass
{
public
static
void
main(String[] args)
throws
IOException
{
boolean
bool =
false
;
// Creating new FilePermissions("Path", "action")
FilePermission FP_obj1 =
new
FilePermission(
"GEEKS"
,
"read"
);
FilePermission FP_obj2 =
new
FilePermission(
"ABC"
,
"write"
);
FilePermission FP_obj3 =
new
FilePermission(
"GEEKS"
,
"read"
);
// Use of equals method
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);
}
}
Output :
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 : ----------
Implementation :
// Java Program illustrating getActions() method
import
java.io.*;
public
class
NewClass
{
public
static
void
main(String[] args)
throws
IOException
{
// Creating new FilePermissions
FilePermission FP_obj1 =
new
FilePermission(
"GEEKS"
,
"read, delete, write"
);
FilePermission FP_obj2 =
new
FilePermission(
"ABC"
,
"write, read, execute"
);
FilePermission FP_obj3 =
new
FilePermission(
"GEEKS"
,
"delete, readlink, read"
);
// Use of getActions() method
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);
}
}
Output :
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 : ----------
Implementation :
// Java Program illustrating hashCode() method
import
java.io.*;
public
class
NewClass
{
public
static
void
main(String[] args)
throws
IOException
{
// Creating new FilePermissions
FilePermission FP_obj1=
new
FilePermission(
"GEEKS"
,
"read, delete, write"
);
// Use of hashCode() method
int
i = FP_obj1.hashCode();
System.out.println(
"hashCode value for FP_obj1 : "
+ i);
}
}
Output :
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 : ----------
Implementation :
// Java Program illustrating implies() method
import
java.io.*;
public
class
NewClass
{
public
static
void
main(String[] args)
throws
IOException
{
// Creating new FilePermissions
FilePermission FP_obj1 =
new
FilePermission(
"GEEKS"
,
"read"
);
FilePermission FP_obj2 =
new
FilePermission(
"ABC"
,
"write"
);
FilePermission FP_obj3 =
new
FilePermission(
"GEEKS"
,
"delete"
);
// Use of implies() method
boolean
check = FP_obj1.implies(FP_obj2);
System.out.println(
"Using implies() for FP_obj1 : "
+ check);
// Checked here with the same FilePermission object
check = FP_obj2.implies(FP_obj2);
System.out.println(
"Using implies() for FP_obj2 : "
+ check);
}
}
Output :
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 : ----------
Implementation :
// Java Program illustrating newPermissionCollection() method
import
java.io.*;
import
java.security.PermissionCollection;
public
class
NewClass
{
public
static
void
main(String[] args)
throws
IOException
{
// Creating new FilePermissions
FilePermission FP_obj1 =
new
FilePermission(
"GEEKS.txt"
,
"read"
);
// Creating new PermissionCollection
// Use of newPermissionCollection()
PermissionCollection FP = FP_obj1.newPermissionCollection();
// Collecting the Permissions of FP_obj1 for FP
FP.add(FP_obj1);
boolean
check = FP.implies(
new
FilePermission(
"GEEKS.txt"
,
"read"
));
System.out.println(
"Is newPermissionCollection() working : "
+ check);
}
}
Output :
Is newPermissionCollection() working : true
This article is contributed by Mohit Gupta 🙂. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.