位置:首頁 > Java技術 > Java.util包 > java.util.PropertyPermission.newPermissionCollection()方法實例

java.util.PropertyPermission.newPermissionCollection()方法實例

java.util.PropertyPermission.newPermissionCollection() 方法創建並返回newPermissionCollection對象。

聲明

以下是java.util.PropertyPermission.newPermissionCollection()方法的聲明

public PermissionCollection newPermissionCollection()

參數

NA

返回值

此方法返回新創建的PermissionCollection對象。

異常

NA

例子

下麵的示例演示java.util.PropertyPermission.newPermissionCollection()方法的用法。

package com.yiibai;

import java.security.PermissionCollection;
import java.util.PropertyPermission;

public class PropertyPermissionDemo {
   private static PermissionCollection permissions;
   
   public static void main(String[] args) {
      // Build property permissions collection
      PropertyPermission permission = 
         new PropertyPermission("java.*", "read");
      permissions = permission.newPermissionCollection();
      permissions.add(permission);
      permissions.add(new PropertyPermission("java.home.*", "read,write"));
      
      // Check permissions
      checkFileReadPermission("java.home");
      checkFileWritePermission("java.home");
      checkFileReadPermission("java.home.usr");
      checkFileWritePermission("java.home.usr");
   }

   private static void checkFileReadPermission(String path) {
      // Check permission on read action
      if(permissions.implies(new PropertyPermission(path, "read"))) {
         System.out.println("Has permissions on "+path+" for read");
      }
   }
   
   private static void checkFileWritePermission(String path) {
      // Check permission on read action
      if(permissions.implies(new PropertyPermission(path, "write"))) {
         System.out.println("Has permissions on "+path+" for write");
      }
   }
}

讓我們來編譯和運行上麵的程序,這將產生以下結果:

Has permissions on java.home for read
Has permissions on java.home.usr for read
Has permissions on java.home.usr for write