集册 Java实例教程 检查两条路径是否指向同一文件

检查两条路径是否指向同一文件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

532
检查两个路径是否指向同一个文件
/*时   代    Java - nowjava.com 提 供*/

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 {

      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 ? " 
展开阅读全文