java path类,使用Path类在Java中的两个路径之间创建路径

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

A relative path cannot be constructed if only one of the paths

includes a root element. If both paths include a root element, the

capability to construct a relative path is system dependent.

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?

for example :

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); }

}

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?

解决方案

Because p1 and p3 has different root.

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

// can only relativize paths of the same type

if (this.type != other.type)

throw new IllegalArgumentException("'other' is different type of Path");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值