使用 java.nio.file.Path 实例和字符串导航到子路径
Using a java.nio.file.Path instance and a string to navigate to a sub-path
如何使用 java.nio.file.Path
对象导航到子路径?
我原以为 path = path.subFolder(string)
之类的东西可以工作,其中 string
指定了一个相对于初始 path
.
的子文件夹
但是好像没有这样的方法。
在"I go in and out of a string"之前,我想检查一下我是否遗漏了什么。
您可以在 java.nio.file.Paths
class:
中使用 #get()
实用方法
Paths.get(String first, String... more)
这被记录为:
Converts a path string, or a sequence of strings that when joined form a path string, to a Path.
您正在寻找 Path.resolve(other)
.
引用其 Javadoc:
For example, suppose that the name separator is "/"
and a path represents "foo/bar"
, then invoking this method with the path string "gus"
will result in the Path "foo/bar/gus"
.
示例代码:
Path path = Paths.get("/foo/bar");
Path subFolder = path.resolve("gus"); // represents the path "/foo/bar/gus"
如何使用 java.nio.file.Path
对象导航到子路径?
我原以为 path = path.subFolder(string)
之类的东西可以工作,其中 string
指定了一个相对于初始 path
.
但是好像没有这样的方法。
在"I go in and out of a string"之前,我想检查一下我是否遗漏了什么。
您可以在 java.nio.file.Paths
class:
#get()
实用方法
Paths.get(String first, String... more)
这被记录为:
Converts a path string, or a sequence of strings that when joined form a path string, to a Path.
您正在寻找 Path.resolve(other)
.
引用其 Javadoc:
For example, suppose that the name separator is
"/"
and a path represents"foo/bar"
, then invoking this method with the path string"gus"
will result in the Path"foo/bar/gus"
.
示例代码:
Path path = Paths.get("/foo/bar");
Path subFolder = path.resolve("gus"); // represents the path "/foo/bar/gus"