Java Files check if two Path objects represent the same file

Description

Java Files check if two Path objects represent the same file

import java.io.IOException;
import java.nio.file.Files;
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:\\MyData\\main.java");

    Path p3 = Paths.get("C:\\myData\\..\\myData\\Main.java");


    boolean b1 = p1.equals(p2);
    System.out.println(b1);/*  w  w  w .  j  a v  a  2s  .c  om*/

    boolean b2 = p1.equals(p3);
    System.out.println(b2);

    try {
      System.out.println(Files.isSameFile(p1, p3) );
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}



PreviousNext

Related