Java FilePermission Class
Last Updated :
10 Jan, 2025
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
// 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
// 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
// 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
// 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
// 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:
Similar Reads
Java File Class Java File class is a representation of a file or directory pathname. Because file and directory names have different formats on different platforms, a simple string is not adequate to name them. Java File class contains several methods for working with the pathname, deleting and renaming files, crea
6 min read
java.nio.file.LinkPermission Class in Java The java.nio.file.LinkPermission class handles permissions for link creation operations. The permission allowed by these class are as follows: Permission name What permission allows Risk of granting this permission hard This permission allows adding an existing file to a directory. This operation is
3 min read
java.net.NetPermission Class in Java NetPermission class is used to allow network permissions. NetPermission class extends BasicPermission class. It is a ânamedâ permission i.e it contains a name but no action. Permission nameWhat permission allowsRisks associated with this permissionallowHttpTraceThis permission allows using the HTTP
4 min read
Java.util.PropertyPermission class in Java This class is for property permission. It extends BasicPermission. The name is the name of the property ("java.home", "os.name", etc). The naming convention follows the hierarchical property naming convention. Also, an asterisk may appear at the end of the name, following a ".", or by itself, to sig
4 min read
File Permissions in Java Java provides a number of method calls to check and change the permission of a file, such as a read-only file can be changed to have permissions to write. File permissions are required to be changed when the user wants to restrict the operations permissible on a file. For example, file permission ca
3 min read