• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

using relativize() method

 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my current directory is : C:\myprograms
we have some rules for this method:
1. using relativize() to construct a path between a two absolute paths having different roots (say, C:\ and D:\) would throw IlliegalArgumentException.
2. using relativize() to construct a path between absolute path and relative path would throw IlliegalArgumentException.
Ok that's a rule. I agree. But what if say :


now my question is this, since the line1 proves that the path is an absolute path and line2 proves that it is under the Root C:\ . Both these fulfills the conditions required as i mentions above in point 1 and 2 . Both paths are absolute and both belongs to the same Root i.e C:\ . Then why IlliegalArgumentException ?
 
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read documentation before posting.

https://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html#relativize%28java.nio.file.Path%29

A relative path cannot be constructed if only one of the paths have a root component. Where both paths have a root component then it is implementation dependent if a relative path can be constructed.



So change /java to C:/java and it will work. The fact that toAbsolutePath() gives C:\java is irrelevant.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with the documentation

Ahmed Bin S wrote:
So change /java to C:/java and it will work. The fact that toAbsolutePath() gives C:\java is irrelevant.


I know it will work .
But that irrelevant C:\java is also a valid place in memory . we can easily store file to this irrelevant place.
Then why relativize() consider the two paths different , one having C:\ and one without C:\. As my question is ?
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a design decision. The designers make decisions so that the language is easy to use and to reduce the probability of errors - allegedly!

You have to realise that neither toAbsolutePath() nor relativize() check the actual path+file exist. See below. If they don't care whether it exists or not, then it is irrelevant that you actually have a C:\java on your system.

These methods operate at the "logical" and not the "physical" level. If you want to use relativize() then it is up to you to create a Path object so that it is C:\java, and then call relativize() on that.





U1@Ahmed MINGW64 ~
$ java Test
Absolute path of p1: C:\a\b
Absolute path of p2: C:\a\b\c\d
Relative path of p2 to p1: c\d

U1@Ahmed MINGW64 ~
$ ls -ld C:/a/b
ls: cannot access C:/a/b: No such file or directory

U1@Ahmed MINGW64 ~
$ ls -ld C:/a/b/c/d
ls: cannot access C:/a/b/c/d: No such file or directory


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic