I know the differences between the two by reading some related text. But I am curious that why getCanonicalPath()
throws checked exception while getAbsolutePath()
does not throw any?
A canonical path is always an absolute path. Converting from a path to a canonical path makes it absolute (usually tack on the current working directory so e.g. ./file. txt becomes c:/temp/file. txt ).
The getCanonicalPath() method is a part of Path class. This function returns the Canonical pathname of the given file object. If the pathname of the file object is Canonical then it simply returns the path of the current file object. The Canonical path is always absolute and unique, the function removes the '.
But I am curious that why
getCanonicalPath()
throws checked exception whilegetAbsolutePath()
does not throw any?
It says why in the documentation for getCanonicalPath
:
IOException
- If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With