PermissionCollection newPermissionCollection


描述

所述java.io.FileOutputStream.newPermissionCollection()返回存储FilePermission对象的一个新的PermissionCollection对象。

声明

以下是java.io.FilePermission.newPermissionCollection()方法的声明

public PermissionCollection newPermissionCollection()

参数

p - 允许检查

返回值

该方法返回适用于stroing FilePermissions的新PermissionCollection对象。

异常

NA

实例

以下示例显示了java.io.FilePermission.newPermissionCollection()方法的用法。

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");
      }
   }
}

让我们编译并运行上面的程序,这将产生以下结果

Permission for C://test.txt is read