Java I/O How to - Combine paths using path resolution








Question

We would like to know how to combine paths using path resolution.

Answer

import java.nio.file.Path;
import java.nio.file.Paths;
//  ww w .  j  a va 2  s . c om
public class Main {

  public static void main(String[] args) {
    Path rootPath = Paths.get("c:/home/docs");
    System.out.println(rootPath);
    Path partialPath = Paths.get("users.txt");
    System.out.println(partialPath);
    Path resolvedPath = rootPath.resolve(partialPath);
    System.out.println(resolvedPath);
  }
}

The code above generates the following result.