Java Filepermission Class

The Java FilePermission class encapsulates permissions associated with a directory or file. Each permission is associated with a specific path, which can fall into one of two categories:

1) When a permission is assigned to D:\\IO\\ -: it means that it applies to all subdirectories and files in a recursive manner.

2) When seeing "D:\\IO\\*", it signifies that the authorization is linked to every directory and file contained within this specific directory, except for any subdirectories.

Java FilePermission class declaration

Let's examine the declaration of the Java.io.FilePermission class:

Example

public final class FilePermission extends Permission implements Serializable

Methods of FilePermission class

Method Description
ByteArrayOutputStream() Creates a new bytearrayoutput 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 anobject.
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.

Java FilePermission Example

Consider the following straightforward scenario where read permission is assigned to a directory path and write permission is granted to a file within that directory.

Example

package com.example;



import java.io.*;

import java.security.PermissionCollection;

public class FilePermissionExample{

	   public static void main(String[] args) throws IOException {

	    String srg = "D:\\IO Package\\java.txt";

	    FilePermission file1 = new FilePermission("D:\\IO Package\\-", "read");

	    PermissionCollection permission = file1.newPermissionCollection();

	    permission.add(file1);

	         FilePermission file2 = new FilePermission(srg, "write");

	         permission.add(file2);

		     if(permission.implies(new FilePermission(srg, "read,write"))) {

	         System.out.println("Read, Write permission is granted for the path "+srg );

  	         }else {

  	        System.out.println("No Read, Write permission is granted for the path "+srg);  	         }

	   } 

}

Output

Output

Read, Write permission is granted for the path D:\IO Package\java.txt

Input Required

This code uses input(). Please provide values below: