了解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實例,也可以是表示路徑的字符串 。
此方法針對此路徑解析另一條路徑。 解決方法如下:
下面的測試中給出了可以調用此方法的不同方案:
@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 absoluteassertThat(path.resolveSibling(other)).isEqualTo(Paths.get("src", "main", "resources", "test2"));//other path is absoluteassertThat(path.resolveSibling("/test2")).isEqualTo(Paths.get("/test2"));//this path has no parentpath = Paths.get("/");assertThat(path.resolveSibling("/test2")).isEqualTo(Paths.get("/test2"));//the other path is empty and this path has no parentassertThat(path.resolveSibling("")).isEqualTo(Paths.get(""));//the other path is empty and this path has parentpath = 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 absolutePath 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 samepath = Paths.get("/src", "main", "resources", "test1");other = Paths.get("/src", "main", "resources", "test1");assertThat(path.startsWith(other)).isTrue();//either of them is relativepath = Paths.get("src", "main", "resources", "test1");other = Paths.get("/src", "main", "resources", "test1");assertThat(path.startsWith(other)).isFalse();//both of them are relativepath = 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
總結
以上是生活随笔為你收集整理的了解java.nio.file.Path – 2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: eosm魔灯(eosm魔灯设置教程)
- 下一篇: camel.js_Camel 2.11