Search code examples
javapathnio

Creating a child NIO.2 Path from a parent Path instance


How do I create a java.nio.file.Path instance denoting a child path, if I already have an instance pointing to the parent directory on the same file system?

Currently, I'm using

final Path parent = Paths.get("usr", "local");
final Path child = Paths.get(parent.toString(), "bin");

-- but this doesn't look very elegant.

What I'm looking is some sort of a java.io.File(File parent, String child) constructor for NIO.2.


Solution

  • You can write child = parent.resolve("bin");

    resolve can take Path or String as argument.