Java - FilePermission equals(Object obj) method



Description

The Java FilePermission equals(Object obj) method is used to check if two FilePermission objects represent the same permission (i.e., they have the same file path and actions).

Declaration

Following is the declaration for java.io.FilePermission.equals(Object obj) method −

public boolean equals(Object obj)

Parameters

obj − The object to be tested for equality with this object.

Return Value

This method returns true if the obj is a FilePermission, and has the same pathname and actions as this object.

Exception

NA

Example - Usage of FilePermission equals(Object obj) method

The following example shows the usage of Java FilePermission equals(Object obj) method.

FilePermissionDemo.java

package com.tutorialspoint;

import java.io.FilePermission;
import java.io.IOException;

public class FilePermissionDemo {
   public static void main(String[] args) throws IOException {
      FilePermission fp = null;
      FilePermission fp1 = null;
      FilePermission fp2 = null;
      FilePermission fp3 = null;
      boolean bool = false;
      
      try {
         // create new file permissions
         fp = new FilePermission("test.txt", "read");
         fp1 = new FilePermission("test1.txt", "read");
         fp2 = new FilePermission("test.txt", "write");
         fp3 = new FilePermission("test.txt", "read");
         
         // checks two file permission objects for equality
         bool = fp.equals(fp1);

         // prints
         System.out.println(bool);
         
         // checks two file permission objects for equality
         bool = fp.equals(fp2);
         
         // prints
         System.out.println(bool);
         
         // checks two file permission objects for equality
         bool = fp.equals(fp3);
         
         // prints
         System.out.print(bool);
         
      } catch(Exception ex) {
         // if an error occurs
         ex.printStackTrace();
      } finally {
         
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

false
false
true

Example - Comparing Two Identical FilePermission Objects

The following example shows the usage of Java FilePermission equals(Object obj) method.

FilePermissionDemo.java

package com.tutorialspoint;

import java.io.FilePermission;

public class FilePermissionDemo {
    public static void main(String[] args) {
       // Creating two identical FilePermission objects
      FilePermission fp1 = new FilePermission("C:\\Users\\Documents\\file.txt", "read,write");
      FilePermission fp2 = new FilePermission("C:\\Users\\Documents\\file.txt", "read,write");

      // Checking equality
      if (fp1.equals(fp2)) {
         System.out.println("Both FilePermission objects are equal.");
      } else {
         System.out.println("FilePermission objects are not equal.");
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Both FilePermission objects are equal.

Explanation

  • Two FilePermission objects (fp1 and fp2) are created with the same file path and same actions (read,write).

  • The equals() method is used to compare them.

  • Since both objects are identical, it prints: Both FilePermission objects are equal.

Example - Comparing Different FilePermission Objects

The following example shows the usage of Java FilePermission equals(Object obj) method.

FilePermissionDemo.java

package com.tutorialspoint;

import java.io.FilePermission;

public class FilePermissionDemo {
   public static void main(String[] args) {
      // Creating two different FilePermission objects
      FilePermission fp1 = new FilePermission("C:\\Users\\Documents\\file.txt", "read");
      FilePermission fp2 = new FilePermission("C:\\Users\\Documents\\file.txt", "write");

      // Checking equality
      if (fp1.equals(fp2)) {
         System.out.println("Both FilePermission objects are equal.");
      } else {
         System.out.println("FilePermission objects are not equal.");
      }
   }
}

Output()

Let us compile and run the above program, this will produce the following result−

FilePermission objects are not equal.

Explanation

  • Two FilePermission objects (fp1 and fp2) are created for the same file path but different actions (read vs. write).

  • The equals() method is used to compare them.

  • Since the actions are different, the method returns false, and the output is: FilePermission objects are not equal.

java_io_filepermission.htm
Advertisements