Java FilePermission Class

Last Updated : 6 Oct, 2026

The FilePermission class in the java.io package represents permission to access a file or directory using operations such as reading, writing, executing, deleting, and reading symbolic links. It was designed for Java's former Security Manager permission model.

  • FilePermission is deprecated for removal since Java 25.
  • It is now mainly useful for understanding and maintaining legacy Java applications that used the Security Manager.

Note: The Security Manager is no longer supported. Therefore, FilePermission should not be used as a modern mechanism for restricting file-system access.

Java
import java.io.FilePermission;

public class Main {
    public static void main(String[] args) {
        FilePermission fp = new FilePermission("GEEKS.txt", "read");
        System.out.println(fp.getActions());
    }
}

Output:

read

Explanation:

  • The FilePermission object is created with the action "read".
  • The getActions() method returns the canonical string representing the allowed actions.
  • Since only one action is specified, it simply prints read.

FilePermission

FilePermission is a final class that extends java.security.Permission. A FilePermission object contains:

  • A file or directory path.
  • One or more actions associated with that path.

For example:

FilePermission permission = new FilePermission("GEEKS.txt", "read,write");

This object represents read and write permissions for GEEKS.txt.

However, these objects no longer provide a supported mechanism for enforcing file access in current Java versions because the Security Manager functionality has been discontinued.

Supported Actions

A FilePermission object can represent the following file-access actions:

  • read: Represents permission to read a file or directory.
  • write: Represents permission to write to or modify a file.
  • execute: Represents permission to execute a file.
  • delete: Represents permission to delete a file or directory.
  • readlink: Represents permission to read the target of a symbolic link.

Class Declaration of FilePermission Class

public final class FilePermission extends Permission implements Serializable

Constructor

Creates a new FilePermission object with the specified actions.

FilePermission(String path, String actions)

Parameters:

  • path: The file or directory path.
  • actions: Actions to be allowed (e.g., "read", "write").

Methods of FilePermission Class

Important methods provided by FilePermission include:

1. equals(Object obj)

Used to compare two FilePermission objects for equality. It returns true only when both the file path and the permitted actions are exactly the same.

Syntax:

public boolean equals(Object obj)

  • Parameters: obj - the FilePermission object to compare.
  • Returns: true if equal, otherwise false.
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
    }
}

Output
true

2. getActions()

Provides a standardized (canonical) string of all actions granted to the permission. The actions are returned in a fixed order and separated by commas.

Syntax:

public String getActions()

Returns: Canonical string: representing the actions associated with the object.

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
    }
}

Output
read,write

3. hashCode()

Generates a hash value that represents the FilePermission object. This value is consistent with equals() and is useful when permissions are stored in hash-based collections.

Syntax:

public int hashCode()

Returns: Returns the hash code value for the FilePermission object.

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
    }
}

Output
-989797644

Note: The exact hash code should not be treated as a fixed value.

4. implies(Permission arg)

Determines whether the current permission fully covers another permission. It returns true if the current object allows all actions requested by the given permission.

Syntax:

public boolean implies(Permission p)

  • Parameters:p - the permission to check.
  • Returns:true if the permission is implied, otherwise false.
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
    }
}

Output
true

5. newPermissionCollection()

The newPermissionCollection() method creates a PermissionCollection designed for storing FilePermission objects. It is primarily relevant to the legacy permission model associated with Java's former Security Manager architecture.

Syntax:

public PermissionCollection newPermissionCollection()

Returns: A PermissionCollection that can store FilePermission objects.

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
    }
}

Output
true
Comment