![]() |
VOOZH | about |
Prior to discussing the differences lets quickly recap all three methods
1. getPath() Method
getPath() is a method of URL class.It converts the given abstract pathname into a pathname string. The resulting string uses the default name-separator character to separate the names in the name sequence.
Returns: The string form of an abstract pathname
Example
Output:
2. getAbsolutePath() Method
getAbsolutePath() returns a path object representing the absolute path of the given path. If the given pathname is already absolute, then the pathname string is simply returned as if by the getPath() method. If this abstract pathname is the empty abstract pathname then the pathname string of the current user directory, which is named by the system property user '.dir'(), is returned. Otherwise, this pathname is resolved in a system-dependent way.
Returns: The absolute pathname string denoting the same file or directory as this abstract pathname
Example:
Output:
3. getCanonicalPath() Method
This method returns the canonical pathname string of the given abstract pathname. A canonical pathname is both absolute and unique. This method first converts the pathname to absolute form if necessary, as if by invoking the getAbsolutePath() method, and then maps it to its unique form in a system-dependent way.
Returns: The canonical pathname string denoting the same file or directory as the abstract pathname.
Output:
Outputs explanation: In order to interpret better with these CMD outputs or hardcoded outputs the specified location of java files used are as follows
:C:\Users\ASPIRE\Desktop\Java\getPathExample or getAbsoltePathExample or getCanonicalPathExample
Location of demo.txt file
:C:\Users\ASPIRE\Desktop\Java\Notes\Chapter one\demo.txt
Now after discussing each of them lets dive on to differences between getPath(), getAbsolutePath(), getCanonicalPath()which are as follows:
| getPath() | getAbsolutePath() | getCononicalPath() |
|---|---|---|
| This method returns a string that denotes (absolute or relative) pathname of the file represented by the file object. | This method returns the absolute pathname string of the abstract file pathname. | This method returns the canonical pathname string of the given abstract pathname. |
| If the file object is created using a relative path then the path returned is a relative path. | If the abstract pathname is relative, then it is resolved in a system-dependent way. | If the file object is created using a relative path then this method first converts pathname to absolute and maps it to a unique form. |
| If the file object is created using the absolute path then the path returned is the absolute path. | If the abstract pathname is already absolute, then the same path name string is returned. | If the file object is created using a relative path then the path returned will be in unique form. |
| This method does not resolve pathname. | This method only resolves the current directory for a relative path. Shorthand representations (such as โ.โ and โ..โ) are not resolved further. | This method involves removing redundant names such as โ.โ and โ..โ from the pathname, resolving symbolic links (On Unix platform), and converting drive letters to a standard case (On Microsoft Windows platform). |
|
Example On window's System File path= new File(โNotes/../demo.txt"); Output: Notes\..\demo.txt On Unix's System File path = new File("Notes/../demo.txt ") Output: Notes/../demo.txt |
Example Window's System File path= new File(โNotes/../demo.txtโ); Output: C:\Users\ASPIRE\Desktop\Java\Notes\..\demo.txt On Unix's System File path = new File(โNotes/../demo.txt โ) Output: home/pooja/Desktop/Notes/../demo.txt |
Example On Window's System File path= new File(โNotes/../demo.txtโ); Output: C:\Users\ASPIRE\Desktop\Java\demo.txt On Unix's System File path = new File("Notes/../demo.txt โ) Output: /home/pooja/Desktop/Java/demo.txt |