Path.resolve(其他)java.nio.file.Path方法

前端之家收集整理的这篇文章主要介绍了Path.resolve(其他)java.nio.file.Path方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
直接来自这个 API

resolve

Path resolve(Path other)

Resolve the given path against this path.

If the other parameter is an absolute path then this method trivially
returns other. If other is an empty path then this method trivially
returns this path. Otherwise this method considers this path to be a
directory and resolves the given path against this path. In the
simplest case,the given path does not have a root component,in which
case this method joins the given path to this path and returns a
resulting path that ends with the given path. Where the given path has
a root component then resolution is highly implementation dependent
and therefore unspecified.

(强调我的)

这里有一点矛盾,首先他们说:

>如果另一个参数是绝对路径,那么这个方法
平凡地返回其他.

然后他们说:
>如果给定路径具有根组件,则解决方案高度依赖于实现,因此未指定.

绝对路径是否必须包含根组件才能成为这样的组件?
提前致谢.

解决方法

对你的问题的简短回答是否定的,绝对路径不需要有根组件,但是,取决于提供者,它可能.

如果我们看一下source code for UnixPath,我们确实看到,如果它是一个绝对路径,那么它将返回一个根组件,它将只返回一个根组件.

但是,并不要求以这种方式实施.理论上至少,getRoot()可以返回一些东西,而isAbsolute()则返回false.在此,结果是不确定的.或者,将其放在真值图表格中:

Result of resolve() when:
                     getRoot()==null  getRoot()!=null
isAbsolute()==true   defined          defined
isAbsolute()==false  defined          undefined

猜你在找的Java相关文章