Java FilePermission Class
java.io.FilePermission class represents access to a file or directory. These accesses are in the form of a path name and a set of actions associated with the path name(specifies which file to open along with the extension and the path).
Example: In FilePermission(“GEEKS.txt”, “read”) “GEEKS.txt” is the path, and “read” is the action being performed.
Supported Actions:
- read: Grants permission to read the file.
- write: Grants permission to write to the file.
- delete: Grants permission to delete the file by calling File.delete
- readlink: Grants permission to read symbolic links.
- execute: Grants permission to execute the file.
Class Declaration of FilePermission Class
public final class FilePermission
extends Permission
implements Serializable
Constructor of FilePermission Class
FilePermission(String p, String a): Creates a new file permission object with “a” action.
Methods of FilePermission Class

1. equals(Object FP_obj)
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.
Example:
// Java Program illustrating equals() method
import java.io.*;
public class EqualsMethod
{
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:
2. getActions()
Syntax:
public String getActions()
Returns: Canonical string: representing the actions associated with the object.
Example:
// Java Program illustrating getActions() method
import java.io.*;
public class GetActions
{
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:
3. hashCode()
Syntax:
public int hashCode()
Returns: hash code value for the argumented object
Example:
// Java Program illustrating hashCode() method
import java.io.*;
public class HashCodeMethod
{
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:
4. implies(Permission arg)
Syntax:
public boolean implies(Permission arg)
- Parameters: arg: Permission to be checked
- Returns: true if the FilePermission object has the argumented Permission else, false
Example:
// Java Program illustrating implies() method
import java.io.*;
public class ImpliesMethod
{
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:
5. newPermissionCollection()
Syntax:
public PermissionCollection newPermissionCollection()
- Parameters: arg: Permission to be checked
- Returns: new PermissionCollection object having the FilePermission objects.
Example:
// Java Program illustrating
// newPermissionCollection() method
import java.io.*;
import java.security.PermissionCollection;
public class NewPermissionCollection
{
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: