Java NIO-Files.isSameFile与Path.equals有何不同 [英] Java NIO - How is Files.isSameFile different from Path.equals

查看:201
本文介绍了Java NIO-Files.isSameFile与Path.equals有何不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白java.nio.file.Files.isSameFile方法与java.nio.file.Path.equals方法有何不同.

有人可以告诉他们有什么不同吗?

解决方案

它们非常不同.

例如:

final Path p1 = Paths.get("/usr/src");
final Path p2 = Paths.get("/usr/../usr/src");

p1.equals(p2); // FALSE
Files.isSameFile(p1, p2); // true

final Path p1 = fs1.getPath("/usr/src");
final Path p2 = fs2.getPath("/usr/src");

p1.equals(p2); // FALSE

一个Path等于另一个Path,当且仅当:

  • 它们具有相同的FileSystem;
  • 它们具有相同的根元素;
  • 它们具有相同的名称元素.

这与Files.isSameFile()完全不同,后者访问文件系统并尝试查看两个Path是否指向相同的文件系统资源.

I could not understand how java.nio.file.Files.isSameFile method is different from java.nio.file.Path.equals method.

Could anybody please tell how they are different?

解决方案

They are very different.

For instance:

final Path p1 = Paths.get("/usr/src");
final Path p2 = Paths.get("/usr/../usr/src");

p1.equals(p2); // FALSE
Files.isSameFile(p1, p2); // true

final Path p1 = fs1.getPath("/usr/src");
final Path p2 = fs2.getPath("/usr/src");

p1.equals(p2); // FALSE

A Path is equal to another Path if and only if:

  • they have the same FileSystem;
  • they have the same root element;
  • they have the same name elements.

This is very different from Files.isSameFile() which accesses the filesystem and tries and see if two Paths point to the same filesystem resource.

这篇关于Java NIO-Files.isSameFile与Path.equals有何不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆