JavaFilePermission

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java FilePermission Class

Java FilePermission class contains the permission related to a directory or file. All the permissions are related with path. The path can be of two types:

1) D:IO-: It indicates that the permission is associated with all sub directories and files recursively.

2) D:IO*: It indicates that the permission is associated with all directory and files within this directory excluding sub directories.

Java FilePermission class declaration

Let's see the declaration for Java.io.FilePermission class:

  1. public final class FilePermission extends Permission implements Serializable  

Methods of FilePermission class

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 File Permission class methods

Method

Description

int hash Code()

It is used to return the hash code value of an object.

String get Actions()

It is used to return the "canonical string representation" of an action.

Boolean equals(Object obj)

It is used to check the two File Permission objects for equality.

Boolean implies(Permission p)

It is used to check the File Permission object for the specified permission.

Permission Collection new Permission Collection()

It is used to return the new Permission Collection object for storing the File Permission object.

 

Java File Permission Example

Let's see the simple example in which permission of a directory path is granted with read permission and a file of this directory is granted for write permission.

  1. package com.javatpoint;  
  2.   
  3. import java.io.*;  
  4. import java.security.PermissionCollection;  
  5. public class FilePermissionExample{  
  6.        public static void main(String[] args) throws IOException {  
  7.         String srg = "D:\IO Package\java.txt";  
  8.         FilePermission file1 = new FilePermission("D:\IO Package\-""read");  
  9.         PermissionCollection permission = file1.newPermissionCollection();  
  10.         permission.add(file1);  
  11.              FilePermission file2 = new FilePermission(srg, "write");  
  12.              permission.add(file2);  
  13.              if(permission.implies(new FilePermission(srg, "read,write"))) {  
  14.              System.out.println("Read, Write permission is granted for the path "+srg );  
  15.              }else {  
  16.             System.out.println("No Read, Write permission is granted for the path "+srg);            }  
  17.        }   
  18. }  

Output

Read, Write permission is granted for the path D:IO Packagejava.txt