It's not a secret anymore!
The moose likes Java in General and the fly likes resolve method java.nio.Path's method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "resolve method java.nio.Path Watch "resolve method java.nio.Path New topic

resolve method java.nio.Path's method

Nick Widelec
Ranch Hand

Joined: Feb 28, 2013
Posts: 226

I thoroughly understand the use of resolve(path) and relativize(path). In the following snippet though there is something which is not very clear to me:

This snippet has been taken from here (preVisitDirectory method). I have slightly modified it as for changing name of target to destinationFolder.

If destinationFolder is /home/user/Desktop/Destination and source and dir (at least for the first recursive call) are /home/user/Desktop/ToBeCopied.

The result of destinationFolder will be /home/user/Desktop/Destination. This works fine and it is supercool however I cannot see why it gives back this outcome since source.relativize(dir); returns ../ToBeCopied and then with destinationFolder.resolve(../ToBeCopied) it should return (without normalizing it) /home/user/Desktop/Destination/../ToBeCopied hence /home/user/Desktop/ToBeCopied. Now if it was the case my program would not work, /ToBeCopied would self copy itself (if it makes sense). What actually happens is that in somehow the system takes the relative pattern used in source.relativize(dir) which is ../ToBeCopied and read something like "go back one step and then comeback" not like "go back one step and then back to /ToBeCopied". Why does it happen though? Logically should give back /home/user/Desktop/Destination/../ToBeCopied instead it returns /home/user/Desktop/Destination. What am I missing? Thanks a lot in advance!

PS: dir is the current directory of the recursive method preVisitDirectory and the first call will be exactly the same as the source itself.

I agree. Here's the link:
subject: resolve method java.nio.Path's method
It's not a secret anymore!