Java Path combine two path values with resolve()

Description

Java Path combine two path values with resolve()


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

public class Main {
  public static void main(String[] args) {
    Path rootPath = Paths.get("/home/docs");
    Path partialPath = Paths.get("users.txt");
    Path resolvedPath = rootPath.resolve(partialPath);
    System.out.println("rootPath: " + rootPath);
    System.out.println("partialPath: " + partialPath);
    System.out.println("resolvedPath: " + resolvedPath);
    System.out.println("Resolved absolute path: " + resolvedPath.toAbsolutePath());
  }//from   w ww .j  a v a2 s .c om

}

Using a String argument with the resolve method

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

public class Main {
  public static void main(String[] args) {
    Path rootPath = Paths.get("/home/docs");
    Path partialPath = Paths.get("users.txt");
    /*from ww  w  . j a va  2 s.  c  om*/
    Path resolvedPath = rootPath.resolve("users.txt"); 
    
    System.out.println("rootPath: " + rootPath);
    System.out.println("partialPath: " + partialPath);
    System.out.println("resolvedPath: " + resolvedPath);
    System.out.println("Resolved absolute path: " + resolvedPath.toAbsolutePath());
  }

}
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));

    System.out.println(p2.resolve(p1));

    System.out.println(p1.resolve(p1));
  }// w  w  w.  j  a  v  a2s . co m
}



PreviousNext

Related