Java.io.FilePermission.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.yiibai; 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