📜  Java Java类

📅  最后修改于: 2022-05-13 01:55:02.160000             🧑  作者: Mango

Java 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 类的方法:

  • equals(Object FP_obj) : Java.io.FilePermission.equals(Object FP_obj)告诉两个(即用这个对象检查FP_obj 路径名和文件名)FilePermission 对象是否相等。
    句法 :
    public boolean equals(Object FP_obj)
    Parameters : 
    FP_obj : the FilePermission object to be verified with this object
    Returns :
    true : if both the objects are equal else, false.
    Exception : 
    ----------
    

    执行 :

    // 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 对象的动作。如果对象有两个动作:删除和读取,则该方法将返回“读取,删除”。
    在这种情况下,此方法返回“规范字符串”: read、write、execute、delete、readlink
    句法 :
    public String getActions()
    Parameters : 
    ----------
    Returns :
    canonical string : representing the actions associated with the object.
    Exception : 
    ----------
    

    执行 :

    // 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 对象的哈希码
    句法 :
    public int hashCode()
    Parameters : 
    --------
    Returns :
    hash code value for the argumented object
    Exception : 
    ----------
    

    执行 :

    // 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
  • 暗示(Permission arg) : Java.io.FilePermission.implies(Permission arg)告诉这个 FilePermision 是否有参数的 Permission。
    句法 :
    public boolean implies(Permission arg)
    Parameters : 
    arg : Permission to be checked 
    Returns :
    true if the FilePermission object has the argumented Permission else, false
    Exception : 
    ----------
    

    执行 :

    // 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 对象。
    句法 :
    public PermissionCollection newPermissionCollection()
    Parameters : 
    arg : Permission to be checked 
    Returns :
    new PermissionCollection object having the FilePermission objects.
    Exception : 
    ----------
    

    执行 :

    // 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