📅  最后修改于: 2020-09-27 04:41:07             🧑  作者: Mango
Java FilePermission类包含与目录或文件相关的权限。所有权限都与路径相关。路径可以有两种类型:
1)D:\\ IO \\-:表示该权限与所有子目录和文件都递归关联。
2)D:\\ IO \\ *:表示该权限与该目录中的所有目录和文件(子目录除外)相关联。
让我们看一下Java.io.FilePermission类的声明:
public final class FilePermission extends Permission implements Serializable
Method | Description |
---|---|
ByteArrayOutputStream() | Creates a new byte array output stream with the initial capacity of 32 bytes, though its size increases if necessary. |
ByteArrayOutputStream(int size) | Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes. |
Method | Description |
---|---|
int hashCode() | It is used to return the hash code value of an object. |
String getActions() | It is used to return the “canonical string representation” of an action. |
boolean equals(Object obj) | It is used to check the two FilePermission objects for equality. |
boolean implies(Permission p) | It is used to check the FilePermission object for the specified permission. |
PermissionCollection newPermissionCollection() | It is used to return the new PermissonCollection object for storing the FilePermission object. |
让我们看一个简单的示例,其中授予目录路径权限和读取权限,并授予该目录文件的写入权限。
package com.javatpoint;
import java.io.*;
import java.security.PermissionCollection;
public class FilePermissionExample{
public static void main(String[] args) throws IOException {
String srg = "D:\\IO Package\\java.txt";
FilePermission file1 = new FilePermission("D:\\IO Package\\-", "read");
PermissionCollection permission = file1.newPermissionCollection();
permission.add(file1);
FilePermission file2 = new FilePermission(srg, "write");
permission.add(file2);
if(permission.implies(new FilePermission(srg, "read,write"))) {
System.out.println("Read, Write permission is granted for the path "+srg );
}else {
System.out.println("No Read, Write permission is granted for the path "+srg); }
}
}
输出量
Read, Write permission is granted for the path D:\IO Package\java.txt