Related Articles

Related Articles

Path equals() method in Java with Examples
  • Last Updated : 16 Jul, 2019

The Java Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a directory.path of an entity could be of two types one is an absolute path and other is a relative path. The absolute path is the location address from the root to the entity while the relative path is the location address which is relative to some other path.

equals() method of java.nio.file.Path used to compare this path for equality with the passed object as a parameter. If the given object is not a Path or is a Path associated with a different FileSystem, then this method returns false. This method returns true if, and only if, the given object is a Path that is identical to this Path.

Syntax:

boolean equals(Object other)

Parameters: This method accepts a single parameter the other object which is used to be compared.

Return value: This method returns true if, and only if, the given object is a Path that is identical to this Path.



Below programs illustrate equals() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// java.nio.file.Path.equals() method
  
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // create object of Paths
        Path path1
            = Paths.get("D:\\eclipse\\configuration"
                        + "\\org.eclipse.update");
  
        Path path2
            = Paths.get("D:\\eclipse\\configuration"
                        + "\\org.eclipse.update");
  
        // compare paths for equality
        boolean response
            = path1.equals(path2);
  
        // print result
        if (response)
            System.out.println("Both are equal");
        else
            System.out.println("Both are not equal");
    }
}

chevron_right


Output:

Both are equal

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// java.nio.file.Path.equals() method
  
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // create object of Paths
        Path path1
            = Paths.get("D:\\eclipse\\configuration"
                        + "\\org.eclipse.update");
  
        Path path2
            = Paths.get("D:\\temp\\Spring");
  
        // compare paths for equality
        boolean response = path1.equals(path2);
  
        // print result
        if (response)
            System.out.println("Both are equal");
        else
            System.out.println("Both are not equal");
    }
}

chevron_right


Output:

Both are not equal

References: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#equals()

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :