aspose file tools*
The moose likes Beginning Java and the fly likes Difference between getAbsolutePath and getCanonicalPath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between getAbsolutePath and getCanonicalPath" Watch "Difference between getAbsolutePath and getCanonicalPath" New topic
Author

Difference between getAbsolutePath and getCanonicalPath

vicky malhotra
Greenhorn

Joined: Feb 05, 2005
Posts: 16
Hi!

What is the difference between getAbsolutePath and getCanonicalPath methods of File class.

Regards,

Vicky
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
A single file can have more than one absolute path, but only one canonical.

That is, if two files have different canonical paths, they are different files. The same is *not* true for absolute paths.

For example, both "C:\foo.txt" and "C:\.\foo.txt" are absolute paths that refer to the same file.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

What is the benefit of this differentiation?
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi CL Gilbert,

I have used Canonical Paths to identify Symbolic links on Unix based systems for my application. Absolute paths won't work in that case right?

I would write down some uncompiled code sample for that here,



Its not probably 100% accurate depiction what I wanted to convey but we should get the idea...

Thanks
Maulin
[ July 11, 2005: Message edited by: Maulin Vasavada ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by CL Gilbert:
What is the benefit of this differentiation?


As far as I can tell, comparing canonical paths is the only way to know wether two File objects reference the same physical file. I'd assume that calculating the canonical path is potentially slower and might be inapropriate to confront the user with, though.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Agree with Ilja. Thats why we have to use canonical path to identify symbolic links in Unix based systems.

Regards
Maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between getAbsolutePath and getCanonicalPath