Combining Two Paths - Java File Path IO

Java examples for File Path IO:Path


Combining two paths allows you to define a fixed root path and append to it a partial path.

Java provides this operation through the resolve() method.

Demo Code

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

public class Main {

    public static void main(String[] args) {             

        //define the fix path
        Path base_1 = Paths.get("C:/folder1/folder2/folder4");
        Path base_2 = Paths.get("C:/folder1/folder2/folder3/test.txt");

        //resolve test.txt file
        Path path_1 = base_1.resolve("test.txt");

        //resolve test2.txt file
        Path path_2 = base_1.resolve("test2.txt");
        //  w w  w.j a  va2 m
        //resolve sibling test2.txt file
        Path path_3 = base_2.resolveSibling("test2.txt");


Related Tutorials