VOOZH about

URL: https://www.geeksforgeeks.org/java/difference-between-getpath-and-getcononicalpath-in-java/

โ‡ฑ Difference between getPath() and getCononicalPath() in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Difference between getPath() and getCononicalPath() in Java

Last Updated : 23 Jul, 2025

Prior to discussing the differences lets quickly recap all three methods

  1. getPath() Method
  2. getAbsolutePath() Method
  3. getCanonicalPath() Method

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: 

๐Ÿ‘ Image

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 

  • On Unix's System: A relative pathname is made absolute by resolving it against the current user directory.
  • On Microsoft System: A relative pathname is made absolute by resolving it against the current directory of the drive named by the pathname if any. If not, it is resolved against the current user directory.

Example: 

Output: 

๐Ÿ‘ Image

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: 

๐Ÿ‘ Image


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


 

Comment
Article Tags: