• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

NIO.2 : understanding the relativize() method (Selikoff, Boyrasky)

 
Greenhorn
Posts: 21
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I'm trying to understand how the relativize() method works but I can't figure it out. Here's a code sample:

Code snippet 1


Code snippet 2


In both cases, I don't understand the output at all. If someone could please help me to clear out that one I have of course read the explanation in the book, but it's still not clear to me.
Thank you,

Helene
 
author & internet detective
Posts: 40747
827
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Helene,
The relativize() method gives you a relative path to get from one path to another. Pretend you are a tiny person in the file system and have to climb from one directory to another.

In code snippet 1, you are starting out on the /Test/cat.html file. Unfortunately to get to the other path, you have to go all the way to the root since no subpaths are in common. So you climb up two directories. That's the ../.. part. Then you go down all the way to the cat.html file under /Users. That's the part which gives you Users/somename/documents/Test/cat.html. Combining gives the output of ../../Users/somename/documents/Test/cat.html

The same logic applies to code snippet 2. Try to imagine how you'd climb/navigate. Does the output make sense now?
 
Helene Shaikh
Greenhorn
Posts: 21
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeanne,

Thank you for the answer. But i'm still missing something related to file structure:
1.

=> why doesn't this output ./cat2.html? . (dot) refers to the current directory. Isn't Test the current directory for both cat.html and cat2.html? If you're inside cat.html and you want to navigate to Test, you just use .(dot) if I'm not mistaken.

2. I don't quite understand your explanation concerning my first code snippet. When you're in /Test/cat.html and you want to go to the root, why do you have to go up 2 directories?

Could you please explain to me in great detail the steps taken to achieve those results? Thank you so much in advance and sorry for the trouble


 
Jeanne Boyarsky
author & internet detective
Posts: 40747
827
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Helene,
Because you aren't at the folder level. You are inside it so have to go up a level just to point to the folder itself.
 
Enthuware Software Support
Posts: 4575
45
  • Likes 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Helene Shaikh wrote:Hi Jeanne,

Thank you for the answer. But i'm still missing something related to file structure:
1.

=> why doesn't this output ./cat2.html? . (dot) refers to the current directory. Isn't Test the current directory for both cat.html and cat2.html? If you're inside cat.html and you want to navigate to Test, you just use .(dot) if I'm not mistaken.

2. I don't quite understand your explanation concerning my first code snippet. When you're in /Test/cat.html and you want to go to the root, why do you have to go up 2 directories?

Could you please explain to me in great detail the steps taken to achieve those results? Thank you so much in advance and sorry for the trouble



I think your confusion arises from the way windows treats files and folders. It treats them differently. But Java paths, like *nix paths, do not make any such distinction. As far as Java path is concerned, cat.html and test are both "files". So instead of thinking of "climbing up" a folder using dot dot, you can think of dot dot as just cancelling out one element of the path. One .. cancels out the element that appears just before it.

In this case, one .. cancels cat.html and then you are left with /Users/somename/documents/Test/, which is common in the other path. So you don't need to cancel out any other element. Thus the result is ../cat2.html


/Users/somename/documents/Test/cat.html + ../cat2.html    <---- cat.html and .. cancel each other out.
=>/Users/somename/documents/Test/cat2.html <--- This is same as the other path. Thus, ../cat2.html is correct.

HTH,
Paul.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic