Get a Path Subpath - Java File Path IO

Java examples for File Path IO:Path

Introduction

You can extract a relative path with the subpath() method.

Demo Code


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

public class Main {

    public static void main(String[] args) {

        Path path = Paths.get("C:", "folder1/folder2/folder4", "test.txt");
        System.out.println("Number of name elements is path: " + path.getNameCount());
        for (int i = 0; i < path.getNameCount(); i++) {
            System.out.println("Name element " + i + " is: " + path.getName(i));
        }//from w w  w.ja v a 2s .c  o  m
        System.out.println("Subpath (0,3): " + path.subpath(0, 3));
    }
}

Result


Related Tutorials