• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Understanding java.nio.file.Path.relativize(Path other)

 
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Forum,
In preparing for my java 8 OCPJP exam, I am playing with Path.relativize().
I know other related questions have been asked.
I have read Path.relativize() javadoc, I have seen examples.
But I still cannot get my head around the following scenario(I use Linux, apologies to window users):
My current working directory for my program is: /home/userspace/workspace/java8/
I have two files:
/home/userspace/workspace/java8/zoo.txt
and
/home/userspace/temp/delete/dictionary.txt

I also have the following program testing Path.relativize():



The output is:
relativePathToZoo.relativize(relativePathToDictionary): ../../../temp/delete/dictionary.txt
relativePathToZoo.relativize(relativePathToDictionary).toAbsolutePath(): /home/userspace/workspace/java8/../../../temp/delete/dictionary.txt
relativePathToZoo.relativize(relativePathToDictionary).toAbsolutePath().normalize(): /home/temp/delete/dictionary.txt
relativePathToDictionary.relativize(relativePathToZoo): ../../../../../zoo.txt
relativePathToDictionary.relativize(relativePathToZoo).toAbsolutePath().normalize(): /zoo.txt


My question, the bit I cannot understand is:
Why does relativePathToDictionary.relativize(relativePathToZoo) output ../../../../../zoo.txt??
When normalized, it would make you think that zoo.txt lives in the root directory.

How does relativize() work out such a deep path?
I understand that relativize() works in relation to the current working directory, so it adds

..

to every path. But I am cannot understand, how it worked out the path to zoo.txt in relation to dictionary.txt.

Thanks in advance,
Lucas


 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic