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