Manipulating paths

suggest change

Joining Two Paths

Paths can be joined using the resolve() method. The path passed has to be a partial path, which is a path that doesn’t include the root element.

Path p5 = Paths.get("/home/");
Path p6 = Paths.get("arthur/files");
Path joined = p5.resolve(p6);
Path otherJoined = p5.resolve("ford/files");
joined.toString() == "/home/arthur/files"
otherJoined.toString() == "/home/ford/files"

Normalizing a path

Paths may contain the elements . (which points to the directory you’re currently in) and ..(which points to the parent directory).

When used in a path, . can be removed at any time without changing the path’s destination, and .. can be removed together with the preceding element.

With the Paths API, this is done using the .normalize() method:

Path p7 = Paths.get("/home/./arthur/../ford/files");
Path p8 = Paths.get("C:\\Users\\.\\..\\Program Files");
p7.normalize().toString() == "/home/ford/files"
p8.normalize().toString() == "C:\\Program Files"

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents