Java.io.FilePermission.newPermissionCollection() Method
Advertisements
Description
The java.io.FileOutputStream.newPermissionCollection() returns a new PermissionCollection object of storing FilePermission objects.
Declaration
Following is the declaration for java.io.FilePermission.newPermissionCollection() method:
public PermissionCollection newPermissionCollection()
Parameters
p -- permission to check against
Return Value
The method returns new PermissionCollection object suitable for stroing FilePermissions.
Exception
NA
Example
The following example shows the usage of java.io.FilePermission.newPermissionCollection() method.
package com.tutorialspoint;
import java.io.FilePermission;
import java.io.IOException;
import java.security.PermissionCollection;
public class FilePermissionDemo {
static FilePermission fp = null;
static PermissionCollection pc = null;
public static void main(String[] args) throws IOException {
try{
// create new file permissions
fp = new FilePermission("C://test.txt", "read");
// create new permission collection
pc = fp.newPermissionCollection();
// add permission to the permission collection
pc.add(fp);
// tests if the file permission is read
TestFileReadPermission("C://test.txt");
}catch(Exception ex){
// if an error occurs
ex.printStackTrace();
}
}
// method to test file permission
public static void TestFileReadPermission(String path)
{
if(pc.implies(new FilePermission("C://test.txt", "read"))) {
System.out.println("Permission for "+path+" is read");
}
}
}
Let us compile and run the above program, this will produce the following result:
Permission for C://test.txt is read