Combining Two Paths - Java File Path IO

Java examples for File Path IO:Path

Introduction

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");
        System.out.println(path_1.toString());

        //resolve test2.txt file
        Path path_2 = base_1.resolve("test2.txt");
        System.out.println(path_2.toString());
        //  w w  w.j a  va2  s.co m
        //resolve sibling test2.txt file
        Path path_3 = base_2.resolveSibling("test2.txt");
        System.out.println(path_3.toString());
    }

}

Related Tutorials