Checking If Two Paths Point to the Same File - Java File Path IO

Java examples for File Path IO:Path

Description

Checking If Two Paths Point to the Same File

Demo Code

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
  public static void main(String[] args) {

    Path path_1 = FileSystems.getDefault().getPath(
        "C:/folder1/folder2/folder4", "my.txt");
    Path path_2 = FileSystems.getDefault().getPath(
        "/folder1/folder2/folder4", "my.txt");
    Path path_3 = FileSystems.getDefault().getPath(
        "/folder1/folder2/dummy/../folder4", "my.txt");
    try {// w  w w  . j  a  va  2  s.co  m
      boolean is_same_file_12 = Files.isSameFile(path_1, path_2);
      boolean is_same_file_13 = Files.isSameFile(path_1, path_3);
      boolean is_same_file_23 = Files.isSameFile(path_2, path_3);

      System.out.println("is same file 1&2 ? " + is_same_file_12);
      System.out.println("is same file 1&3 ? " + is_same_file_13);
      System.out.println("is same file 2&3 ? " + is_same_file_23);
    } catch (IOException e) {
      System.err.println(e);
    }

  }
}

Result


Related Tutorials