了解java.nio.file.Path – 2

在本文的第1部分中,我们研究了java.nio.file.Path类中的大多数API。 在本文中,我们将介绍其余的API。

使用register()

该API允许我们注册java.nio.file.WatchService接口的实现,该接口将侦听目录创建,修改和删除等事件。 并且它通过java.nio.file.WatchKey来激发侦听器。 我想为此API撰写另一篇文章,因为它涉及Java 7中引入的另一个新功能。

使用resolve()

此方法处理两个Path实例。 调用此方法的一个实例resolve()方法,将另一个实例作为参数传递。 参数可以是Path实例,也可以是表示路径字符串

此方法针对此路径解析另一条路径。 解决方法如下:

  1. 如果另一个路径是绝对路径,则它将返回另一个路径。 因为可以使用绝对路径来到达其他路径。
  2. 如果另一个路径是相对路径,则另一个路径将附加到此路径。 例如:
    Path path = Paths.get("src", "main", "resources");
    Path other = Paths.get("blogsamples");
    
    assertThat(path.resolve(other)).isEqualTo(
            Paths.get("src", "main", "resources", "blogsamples"));

下面的测试中给出了可以调用此方法的不同方案:

@Test
public void testResolved() throws IOException {
    Path path = Paths.get("src", "main", "resources");
    Path other = Paths.get("blogsamples");

    assertThat(path.resolve(other)).isEqualTo(
            Paths.get("src", "main", "resources", "blogsamples"));

    other = Paths.get("/Users");
    assertThat(path.resolve(other)).isEqualTo(Paths.get("/Users"));

    path = Paths.get("/src", "main", "resource");
    assertThat(path.resolve("/Users")).isEqualTo(Paths.get("/Users"));
}

使用resolveSibling()

此方法与resolve()类似,不同之处在于它认为此路径的父级可以解析另一个路径。 同样,我在下面的测试中捕获了不同的可能性:

@Test
public void testResolveSibling(){
    Path path = Paths.get("src", "main", "resources", "test1");
    Path other = Paths.get("test2");

    //both paths are not absolute
    assertThat(path.resolveSibling(other)).isEqualTo(
            Paths.get("src", "main", "resources", "test2"));

    //other path is absolute
    assertThat(path.resolveSibling("/test2")).isEqualTo(
        Paths.get("/test2"));

    //this path has no parent
    path = Paths.get("/");
    assertThat(path.resolveSibling("/test2")).isEqualTo(
        Paths.get("/test2"));

    //the other path is empty and this path has no parent
    assertThat(path.resolveSibling("")).isEqualTo(Paths.get(""));

    //the other path is empty and this path has parent
    path = Paths.get("src", "main", "resources", "test1");
    assertThat(path.resolveSibling("")).isEqualTo(
            Paths.get("src", "main", "resources"));
}

使用relativize()

此方法返回一个相对路径,该相对路径在针对该路径解析时将返回另一个路径(即,作为参数传递的路径)。

我试图在下面的测试中说明在尝试在两条路径之间创建相对路径时的不同可能性。

Path path = Paths.get("src", "main", "resources", "test1");
Path other = Paths.get("test2");

assertThat(path.relativize(other).toString())
    .isEqualTo("..\\..\\..\\..\\test2");

在上述情况下,两条路径都是相对的。 它需要从src / main / resources / test1向后4跳才能到达/ test2。 通过应用相对论方法也可以得到相同的结果。

如果其中一个路径是绝对路径,另一个路径是相对路径,则调用relativize会导致IllegalArgumentException ,如下所示:

@Test(expected = IllegalArgumentException.class)
public void testRelativize_WithRelativeAndAbsolutePath(){
    Path path = Paths.get("/src", "main", "resources", "test1");
    Path other = Paths.get("src", "main", "resources");
    path.relativize(other);
}

如果两个路径都是绝对路径,则relativize()的输出取决于实现。 以下测试是针对Windows平台上的JDK 8编写的:

@Test
public void testRelativize_WithAbsolutePaths(){
    Path path = Paths.get("/src", "main", "resources", "test1");
    Path other = Paths.get("/src", "main", "resources", "test1", "test2");
    assertThat(path.relativize(other).toString())
        .isEqualTo("test2");
}

使用startsWith()

此方法检查startsWith()方法所在的路径开头是否与作为参数传递的路径具有相同的名称元素。 并且作为参数传递的路径没有此路径中不存在的多余名称元素。

例如:/ a / b / c以/ a / b开头,a / b / c / d以a / b / c开头

让我们在调用该方法时查看不同的可能情况:

@Test
public void testStartsWith(){
    //both paths are absolute
    Path path = Paths.get("/src", "main", "resources", "test1");
    Path other = Paths.get("/src", "main", "resources");
    assertThat(path.startsWith(other)).isTrue();

    /*
    both paths are absolute, where as the other 
    path has more name elements 
    */
    path = Paths.get("/src", "main", "resources", "test1");
    other = Paths.get("/src", "main", "resources", 
        "test1", "test2");
    assertThat(path.startsWith(other)).isFalse();

    //both paths are same
    path = Paths.get("/src", "main", "resources", "test1");
    other = Paths.get("/src", "main", "resources", "test1");
    assertThat(path.startsWith(other)).isTrue();

    //either of them is relative
    path = Paths.get("src", "main", "resources", "test1");
    other = Paths.get("/src", "main", "resources", "test1");
    assertThat(path.startsWith(other)).isFalse();

    //both of them are relative
    path = Paths.get("src", "main", "resources", "test1");
    other = Paths.get("src", "main", "resources");
    assertThat(path.startsWith(other)).isTrue();

}

翻译自: https://www.javacodegeeks.com/2017/09/getting-know-java-nio-file-path-2.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值