当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.io.FilePermission用法及代码示例


java.io.FilePermission 类用于表示对文件或目录的访问。这些访问采用路径名和与路径名关联的一组操作的形式(指定要打开的文件以及扩展名和路径)。例如,在 FilePermission(“GEEKS.txt”, “read”) 中,“GEEKS.txt” 是路径,“read” 是正在执行的操作。这些行动如下:

  • read : 文件的读取权限
  • write : 对文件的写权限
  • delete : 通过调用File.delete删除文件的权限
  • readlink : 读取链接权限
  • execute : 执行了权限

宣言:

public final class FilePermission
   extends Permission
      implements Serializable

构造函数:

FilePermission(String p, String a) : Creates a new file permission object with "a" action.

FilePermission类的方法:

  • 等于(对象FP_obj):java.io.FilePermission.equals(对象FP_obj)告诉两个(即使用该对象检查 FP_obj 路径名和文件名)FilePermission 对象是否相等。
  • Syntax :
public boolean equals(Object FP_obj)
Parameters : 
FP_obj : the FilePermission object to be verified with this object
返回:
true : if both the objects are equal else, false.
Exception : 
----------
  • 执行:

Java


// Java Program illustrating equals() method
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        boolean bool = false;
        // Creating new FilePermissions("Path", "action")
        FilePermission FP_obj1 = new FilePermission("GEEKS", "read");
        FilePermission FP_obj2 = new FilePermission("ABC", "write");
        FilePermission FP_obj3 = new FilePermission("GEEKS", "read");
        // Use of equals method 
        bool = FP_obj2.equals(FP_obj1);
        System.out.println("Whether FP_obj1 equals FP_obj2 : " + bool);
        bool = FP_obj2.equals(FP_obj3);
        System.out.println("Whether FP_obj2 equals FP_obj2 : " + bool);
        bool = FP_obj1.equals(FP_obj3);
        System.out.println("Whether FP_obj3 equals FP_obj1 : " + bool);
    }
}
  • 输出:
Whether FP_obj1 equals FP_obj2 : false
Whether FP_obj2 equals FP_obj2 : false
Whether FP_obj3 equals FP_obj1 : true
  • getActions(): java.io.FilePermission.getActions()告诉这个FilePermission对象的动作。如果对象有两个操作:删除和读取,则该方法将返回“读取,删除”。在这种情况下,此方法返回“canonical string”:读、写、执行、删除、readlink
  • Syntax :
public String getActions()
Parameters : 
----------
返回:
canonical string : representing the actions associated with the object.
Exception : 
----------
  • Implementation :

Java


// Java Program illustrating getActions() method
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        // Creating new FilePermissions
        FilePermission FP_obj1 = new FilePermission("GEEKS", "read, delete, write");
        FilePermission FP_obj2 = new FilePermission("ABC", "write, read, execute");
        FilePermission FP_obj3 = new FilePermission("GEEKS", "delete, readlink, read");
        // Use of getActions() method 
        String str = FP_obj1.getActions();
        System.out.println("Actions with FP_obj1 : " + str);
        str = FP_obj2.getActions();
        System.out.println("Actions with FP_obj2 : " + str);
        str = FP_obj3.getActions();
        System.out.println("Actions with FP_obj3 : " + str);
    }
}
  • 输出:
Actions with FP_obj1 : read,write,delete
Actions with FP_obj2 : read,write,execute
Actions with FP_obj3 : read,delete,readlink
  • hashCode(): java.io.FilePermission.hashCode()返回带参数的 FilePermission 对象的哈希码
  • Syntax :
public int hashCode()
Parameters : 
--------
返回:
hash code value for the argumented object
Exception : 
----------
  • 执行:

Java


// Java Program illustrating hashCode() method
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        // Creating new FilePermissions
        FilePermission FP_obj1=new FilePermission("GEEKS", "read, delete, write");
        // Use of hashCode() method
        int i = FP_obj1.hashCode();
        System.out.println("hashCode value for FP_obj1 : " + i);
    }
}
  • 输出:
hashCode value for FP_obj1 : 0
  • 暗示(权限参数):java.io.FilePermission.implies(权限参数)告诉 FilePermision 是否具有争论的权限。Syntax :
public boolean implies(Permission arg)
Parameters : 
arg : Permission to be checked 
返回:
true if the FilePermission object has the argumented Permission else, false
Exception : 
----------
  • Implementation :

Java


// Java Program illustrating implies() method
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        // Creating new FilePermissions
        FilePermission FP_obj1 = new FilePermission("GEEKS", "read");
        FilePermission FP_obj2 = new FilePermission("ABC", "write");
        FilePermission FP_obj3 = new FilePermission("GEEKS", "delete");
        // Use of implies() method
        boolean check = FP_obj1.implies(FP_obj2);
        System.out.println("Using implies() for FP_obj1 : " + check);
                 
        // Checked here with the same FilePermission object
        check = FP_obj2.implies(FP_obj2);
        System.out.println("Using implies() for FP_obj2 : " + check);
         
    }
}
  • 输出:
Using implies() for FP_obj1 : false
Using implies() for FP_obj2 : true
  • newPermissionCollection():java.io.FilePermission.newPermissionCollection()创建具有FilePermission 对象的PermissionCollection 对象。Syntax :
public PermissionCollection newPermissionCollection()
Parameters : 
arg : Permission to be checked 
返回:
new PermissionCollection object having the FilePermission objects.
Exception : 
----------
  • Implementation :

Java


// Java Program illustrating newPermissionCollection() method
import java.io.*;
import java.security.PermissionCollection;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        // Creating new FilePermissions
        FilePermission FP_obj1 = new FilePermission("GEEKS.txt", "read");
         
        // Creating new PermissionCollection
        // Use of newPermissionCollection() 
        PermissionCollection FP = FP_obj1.newPermissionCollection();
         
        // Collecting the Permissions of FP_obj1 for FP
        FP.add(FP_obj1);
         
        boolean check = FP.implies(new FilePermission("GEEKS.txt", "read"));
        System.out.println("Is newPermissionCollection() working : " + check);
    }
}
  • 输出:
Is newPermissionCollection() working : true


相关用法


注:本文由纯净天空筛选整理自佚名大神的英文原创作品 Java.io.FilePermission Class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。