Java FilePermission Class
Last Updated :
03 Oct, 2025
The FilePermission class in Java represents access rights to a file or directory. Accesses are defined by a path name and a set of actions associated with that path. This class is part of the java.io package and extends the Permission class while implementing Serializable.
Example:
Java
FilePermission fp = new FilePermission("GEEKS.txt", "read");
Here, "GEEKS.txt" is the path, and "read" is the action permitted.
Supported Actions:
The FilePermission class supports the following actions:
- read: Grants permission to read the file.
- write: Grants permission to modify/write to the file.
- delete: Grants permission to delete the file using File.delete().
- readlink: Grants permission to read symbolic links.
- execute: Grants permission to execute the file.
Class Declaration of FilePermission Class
Java
public final class FilePermission
extends Permission
implements Serializable
Constructor
FilePermission(String path, String actions)
Parameters:
- path: The file or directory path.
- actions: Actions to be allowed (e.g., "read", "write").
Description: Creates a new FilePermission object with the specified actions.
Methods of FilePermission Class
1. equals(Object obj)
Syntax:
public boolean equals(Object obj)
- Description: Checks if the current FilePermission object is equal to the specified object.
- Parameters: obj – the FilePermission object to compare.
- Returns: true if equal, otherwise false.
Example:
Java
import java.io.FilePermission;
public class EqualsExample {
public static void main(String[] args) {
FilePermission fp1 = new FilePermission("GEEKS.txt", "read");
FilePermission fp2 = new FilePermission("GEEKS.txt", "read");
System.out.println(fp1.equals(fp2)); // Output: true
}
}
2. getActions()
Syntax:
public String getActions()
Returns: Canonical string: representing the actions associated with the object.
Example:
Java
import java.io.FilePermission;
public class GetActionsExample {
public static void main(String[] args) {
FilePermission fp = new FilePermission("GEEKS.txt", "read,write");
System.out.println(fp.getActions()); // Output: read,write
}
}
3. hashCode()
Syntax:
public int hashCode()
Returns: Returns the hash code value for the FilePermission object.
Example:
Java
import java.io.FilePermission;
public class HashCodeExample {
public static void main(String[] args) {
FilePermission fp = new FilePermission("GEEKS.txt", "read");
System.out.println(fp.hashCode()); // Output: some integer hash value
}
}
4. implies(Permission arg)
Syntax:
public boolean implies(Permission p)
- Description: Determines whether the current FilePermission object implies the specified permission.
- Parameters:
p – the permission to check. - Returns:
true if the permission is implied, otherwise false.
Example:
Java
import java.io.FilePermission;
import java.security.Permission;
public class ImpliesExample {
public static void main(String[] args) {
FilePermission fp1 = new FilePermission("GEEKS.txt", "read,write");
FilePermission fp2 = new FilePermission("GEEKS.txt", "read");
System.out.println(fp1.implies(fp2)); // Output: true
}
}
5. newPermissionCollection()
Syntax:
public PermissionCollection newPermissionCollection()
- Description: Returns a new PermissionCollection object to hold FilePermission objects.
Example:
Java
import java.io.FilePermission;
import java.security.PermissionCollection;
public class PermissionCollectionExample {
public static void main(String[] args) {
FilePermission fp1 = new FilePermission("GEEKS.txt", "read");
FilePermission fp2 = new FilePermission("GEEKS.txt", "write");
PermissionCollection pc = fp1.newPermissionCollection();
pc.add(fp1);
pc.add(fp2);
System.out.println(pc.elements().hasMoreElements()); // Output: true
}
}
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java