java如何判断两个路径是否相同呢?

重生 Java经验 发布时间:2023-12-30 21:42:41 阅读数:17166 1
下文笔者讲述java代码判断两个路径是否相同的方法分享,如下所示

判断路径是否相同的实现思路

方式1:
   检测两个路径对应的file对象是否相同

方式2:
   使用getCanonicalPath方法检测返回路径
       检测两个路径是否相同

=========================================
    检测两个路径是否相同
        只要一个方式返回true
        则代表两个路径相同
File file1 = new File("D:/test/test.txt");
File file2 = new File("D:/test/test.txt");
boolean isEqual = file1.equals(file2);
System.out.println(isEqual);
  输出结果为true

  File file1 = new File("D:/temp/test.txt");
File file2 = new File("D:/temp/../temp/test.txt");
try {
    String path1 = file1.getCanonicalPath();
    String path2 = file2.getCanonicalPath();
    boolean isEqual = path1.equals(path2);
    System.out.println(isEqual);
} catch(IOException e) {
    e.printStackTrace();
}
输出结果为true
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202312/17039438047613.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者