Java - Paths Comparing for equality

Introduction

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:

Demo

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\\Main.java");
    Path p2 = Paths.get("C:\\TEST\\DATA1.TXT");
    Path p3 = Paths.get("C:\\myData\\..\\myData\\Main.java");
    boolean b1 = p1.equals(p2); 
    boolean b2 = p1.equals(p3); 
  }// w w  w.  j  av  a2  s.  c  o  m
}

Related Topic