The getCanonicalPath() method returns the canonical path of the given file or directory. The precise definition of this method is system-dependent. The method first uses the absolute form of the path and then removes all redundant names such as . and .. from the pathname. It is better to explain a real-life example of how this method works with comparison to getAbsolutePath().
2. Method signature
Parameters:
method does not take any parameter
Returns
String - the canonical pathname string of the specified file or directory
Throws
IOException - if an I/O error occurs
SecurityException - if a required system property value cannot be accessed
3. Examples
3.1. Program that compares getCanonicalPath() and getAbsolutePath() methods
The output:
4. Conclusion
In this article, we presented getCanonicalPath() method that can be used to get the unique form of the file or directory. Note that this method works differently on UNIX and Windows OS.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}