Determine whether two paths are equivalent with equals method in Java

Description

The following code shows how to determine whether two paths are equivalent with equals method.

Example


import java.nio.file.Path;
import java.nio.file.Paths;
/*from ww w. ja va  2s .  c  om*/
public class Main {
  public static void main(String[] args) {
    Path path1 = Paths.get("/home/docs/users.txt");
    Path path2 = Paths.get("/home/docs/users.txt");
    Path path3 = Paths.get("/home/music/A.mp3");

    testEquals(path1, path2);
    testEquals(path1, path3);
  }
  private static void testEquals(Path path1, Path path2) {
    if (path1.equals(path2)) {
      System.out.println("equal");
    } else {
      System.out.println("NOT equal");
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip