Java FilePermission newPermissionCollection()方法

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

1 语法

public PermissionCollection newPermissionCollection()

2 参数

p:要检查的权限

3 返回值

返回一个适合stroing FilePermissions新的PermissionCollection对象。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.FileOutputStream.newPermissionCollection()方法的例子
 */
import java.io.FilePermission;
import java.io.IOException;
import java.security.PermissionCollection;

public class Demo {

    static FilePermission fp = null;
    static PermissionCollection pc = null;

    public static void main(String[] args) throws IOException {
        try{
            // create new file permissions
            fp = new FilePermission("d://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("d://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("d://test.txt", "read"))) {
            System.out.println("Permission for "+path+" is read");
        }
    }
}

输出结果为:

Permission for d://test.txt is read

热门文章

优秀文章