Java - Paths Comparing for equality


You can compare two Path objects for equality based on their textual representation.

The equals() method tests for the equality of two Path objects by comparing their string forms.

Whether the equality test is case-sensitive depends on the file system.

Path.equals() method does not test a Path for existence in the file system.

For example, the path comparison for equality is case-insensitive on Windows. The following code shows how to compare Windows paths:


import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
  public static void main(String[] args) {
    Path p1 = Paths.get("C:\\myData\\");
    Path p2 = Paths.get("C:\\TEST\\DATA1.TXT");
    Path p3 = Paths.get("C:\\myData\\..\\myData\\");
    boolean b1 = p1.equals(p2); 
    boolean b2 = p1.equals(p3); 
  }/*from  w w  w . j a va  2  s  . c om*/

Related Topic