Prior to discussing the differences lets quickly recap all three methods
- getPath() Method
- getAbsolutePath() Method
- 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
Java
import java.io.*;
public class GFG {
public static void main(String[] args)
{
File path1 = new File(
"C:/Users/ASPIRE/Desktop/Java/Notes/Chapter one/demo.txt" );
System.out.println(
"Output for given absolute path : "
+ path1.getPath());
File path2 = new File( "Notes/../demo.txt" );
System.out.println(
"Output for given relative path : "
+ path2.getPath());
}
}
|
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
- 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:
Java
import java.io.*;
public class GFG {
public static void main(String[] args)
{
File path = new File( "Notes/../demo.txt" );
System.out.println( "Output : "
+ path.getAbsolutePath());
}
}
|
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.
Java
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
File path = new File( "Notes/../demo.txt" );
System.out.println( "Output: "
+ path.getCanonicalPath());
}
}
|
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 |