Java - create relative java.nio.Path from two java.nio.Path's
Asked Answered
M

1

5

I have a java.nio.Path which points to an absolute path:

/home/user/project/resources/configuration.xml

I have a second java.nio.Path which points to the root directory of the project, also an absolute path:

/home/user/project

Is it now possible to create a java.nio.Path which holds the relative path between the two:

resources/configuration.xml
Mandelbaum answered 25/11, 2017 at 15:17 Comment(1)
Similar question here: #205284Hallway
A
7

This is precisely what the relativize(Path) method does:

Path confFile = Paths.get("/home/user/project/resources/configuration.xml");
Path rootDir  = Paths.get("/home/user/project");
Path relative = rootDir.relativize(confFile);
Adessive answered 25/11, 2017 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.