本文介绍了使用Path类在Java中的两个路径之间创建路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自的句子的确切含义是什么? oracle java tutorial:

What does exactly mean this sentence from this oracle java tutorial:

使用系统相关 它们只是意味着如果一个元素包含一个根,它只能用于已编写的平台特定语法吗?
我想这是他们唯一的意思。
有没有其他方式可以阅读?

With "system dipendent" do they mean only that if an element contains a root it will work only in the platform specific syntax that has been written?I guess it is the only thing they mean.Are there any other ways of reading that?

例如:

public class AnotherOnePathTheDust {
    public static void main (String []args)
    {
    Path p1 = Paths.get("home");
    Path p3 = Paths.get("home/sally/bar"); //with "/home/sally/bar" i would get an exception.
    // Result is sally/bar
    Path p1_to_p3 = p1.relativize(p3);
    // Result is ../..

    Path p3_to_p1 = p3.relativize(p1);
    System.out.println(p3_to_p1);   }
}

使用/ home / sally / bar获得的例外情况而不是home / sally / bar(没有root)是这一个:

The exception that I get by using "/home/sally/bar" instead of "home/sally/bar" (without root) is this one:

 java.lang.IllegalArgumentException: 'other' is different type of Path

为什么它不起作用?他们的意思是与系统的冲突是什么?

Why does it not work? what is the conflict with the system that they mean?

推荐答案

因为 p1 p3 有不同的根。

如果使用/ home / sally / bar而不是 home / sally / barfor p3 ,然后 p3.getRoot()将返回 / p1.getRoot()为空。

If you use use "/home/sally/bar" instead of "home/sally/bar" for p3, then p3.getRoot() will return / but p1.getRoot() is null.

你会明白为什么你阅读以下代码后得到此异常(来自 Line374-375):

You'll know why you got this exception after you read following codes (comes from http://cr.openjdk.java.net/~alanb/6863864/webrev.00/src/windows/classes/sun/nio/fs/WindowsPath.java-.html Line374-375):

// can only relativize paths of the same type
if (this.type != other.type)
     throw new IllegalArgumentException("'other' is different type of Path");

这篇关于使用Path类在Java中的两个路径之间创建路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-19 00:33
查看更多