Helenesh
Helenesh

Reputation: 4349

NIO.2 : understanding the relativize() method

I'm struggling to understand the java.nio.file.Path.relativize() method. I am also aware that a similar question has been asked, but the answer didn't help me to understand the concept.

Considering the following code snippet:

Path path = Paths.get("/Users/somename/documents/Test/cat.html");
Path path2 = Paths.get("/documents/Test/cat.html");
System.out.println((path.relativize(path2))); //output: ../../../../../documents/Test/cat.html

Can someone explain to me, using a step-by-step approach, how the relativize() method works in this example? Why do I need to go up 5 directories?

Upvotes: 1

Views: 911

Answers (1)

user6749601
user6749601

Reputation:

I'll get back to your example at the end of my answer.

The purpose of relativize() is to be independent from the underlying file system's structure when it comes to adressing/accessing files and folders. You don't have to know in which parent folder/path your app folder is located. You only have to know your app-related file-structure and can use relativize() to access the needed files which may be placed in totally different folders.

Here's an example:

Let's say you have created an app. Your simple folder structure is

/myApp/app/app.java
/myApp/img/picture1.img
/myApp/sounds/ringtones/bell.mp3

Starting the app your working path is

<parent path>/myApp/app/

If you want to access picture1.img now you don't have to know what its real path (from root down to its actual folder) is. Just use the relative path relativize() provides. It goes like this:

Path path = Paths.get("/myApp/app/app.java");
Path path2 = Paths.get("/myApp/img/picture1.img");
System.out.println((path.relativize(path2)));

which results in

../../img/picture1.img

This is the "relative path" from your file app.java to your file picture1.img.

The first path symbol stands for the file app.java itself

The second is for its parent folder /app

You are now in the folder

/myApp 

from where you go back down the ladder following the path

/img/picture1.img

You can get the file using the relative path shown above and you don't have to care about its realPath() which could be, for instance:

/Users/me/apps/myApp/img/picture1.img

It simply doesn't bother you where your app folder is located as long as you know your own folder structure and access your files with relative paths.

Now back to your example

relativize() doesn't know that from your point of view

<parent folder>/documents/Test/cat.html 

is supposed to be the same as

/Users/somename/documents/Test/cat.html

For relativize() it looks like this

/root/Users/somename/documents/Test/cat.html
/root/documents/Test/cat.html 

So, starting from the upper cat.html you go

1) cat.html = ../

2) Test = ../../

3) documents = ../../../

4) somename = ../../../../

5) Users = ../../../../../

Now you've reached root and now you go down the ladder following the second path.

So the output

../../../../../documents/Test/cat.html

is totally correct from relativize()'s point of view.

Did this explanation help you to understand the function of relativize() better?

Upvotes: 6

Related Questions