Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java FilePermission Class

In this tutorial, we will learn about FilePermission class in Java. FilePermission class is used to represent access to a file or a directory. All the permissions are related to the path.

Syntax

The following syntax is the declaration of FilePermission class.

public final class FilePermission extends Permission implements Serializable  

Constructors

Method Description
ByteArrayOutputStream() Creates a new byte array output stream with the initial capacity of 32 bytes, though its size increases if necessary.
ByteArrayOutputStream(int size) Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.

Java FilePermission class Methods

Method Description
int hashCode() It is used to return the hash code value of an object.
String getActions() It is used to return the "canonical string representation" of an action.
boolean equals(Object obj) It is used to check the two FilePermission objects for equality.
boolean implies(Permission p) It is used to check the FilePermission object for the specified permission.
PermissionCollection newPermissionCollection() It is used to return the new PermissonCollection object for storing the FilePermission object.

File permissions can be read, write, delete, readlink and execute.

Java FilePermission Example with getActions() method:

In the following example, we are creating a file myfile.txt with having read, delete, and write permissions. After that, we are checking for the permissions of the given file using getActions() method.

package studytonight;
import java.io.FilePermission;
public class StudyTonight 
{
	public static void main(String args[])
	{
		 FilePermission fp_obj = new FilePermission("myfile.txt", "read, delete, write"); 
		 System.out.print("File permissions for myfile.txt: "+fp_obj.getActions());
	}
}


File permissions for myfile.txt: read,write,delete

Java FilePermission Example with hashCode() Method

In the following example, we are generating hashCode for the FilePermission object which is passed as an argument to this function.

package studytonight;
import java.io.FilePermission;
public class StudyTonight 
{
	public static void main(String args[])
	{
		FilePermission obj=new FilePermission("myfile.txt", "read, delete, write");   
		int hashCode = obj.hashCode(); 
		System.out.println("hashCode value of obj: " + hashCode); 
	}
}


hashCode value of obj: 1700113143

Conclusion

In this tutorial, we learned about FilePermission class in Java. FilePermission class is used to represent access to a file or a directory. All the permissions are related to the path.