In Java, FilePermission class is used to alter the permissions set on a file. Java FilePermission class contains the permission related to a directory or file. All the permissions are related to the path. The path can be of two types:
D:\\IO\\-: It indicates that the permission is associated with all subdirectories and files recursively.
D:\\IO\\*: It indicates that the permission is associated with all directory and files within this directory excluding subdirectories.
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.
package com.javatpoint;
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
Read, Write permission is granted for the path D:\IO Package\java.txt