Java - Paths Resolving and Combination

Introduction

You can combine two paths using the resolve(Path p) method of the Path interface.

It simply combines the two paths and returns the result, so the returned path ends with the specified path.

The path on which this method is invoked is assumed to be a directory.

The following code has some examples of resolving paths on Windows.

Demo

import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
  public static void main(String[] args) {
    Path p1 = Paths.get("C:\\myData");
    Path p2 = Paths.get("Main.java");
    System.out.println(p1.resolve(p2));

    Path p3 = Paths.get("C:\\test.txt");
    System.out.println(p1.resolve(p3));

    Path p4 = Paths.get("");
    System.out.println(p1.resolve(p4));

    Path p5 = Paths.get("myData\\Data");
    Path p6 = Paths.get("test.txt");
    System.out.println(p5.resolve(p6));
  }//from   w  w w .  j  av a  2 s .com
}

Result

Related Topic