1

How can I use a java.nio.file.Path object to navigate to a sub-path?

I'd have thought that something like path = path.subFolder(string) would work where string specifies a sub-folder relative to the initial path.

But there doesn't seem to be such a method available.

Before "I go in and out of a string", I'd like to check if I've missed something.

2 Answers 2

4

You are looking for Path.resolve(other).

Quoting its 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".

Sample code:

Path path = Paths.get("/foo/bar");
Path subFolder = path.resolve("gus"); // represents the path "/foo/bar/gus"
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the #get() utility method in the java.nio.file.Paths class:

Paths.get(String first, String... more)

This is documented as:

Converts a path string, or a sequence of strings that when joined form a path string, to a Path.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.