Open In App

Difference between getPath() and getCononicalPath() in Java

Last Updated : 14 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

Java




// Java Program illustrating the getPath() method
 
// Importing input output classes
import java.io.*;
 
// Class for getPath method()
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a new file object in which input is
        // absolute path as in argument from the user
        File path1 = new File(
            "C:/Users/ASPIRE/Desktop/Java/Notes/Chapter one/demo.txt");
 
        // Print the display the path string for
        // absolute path using getPath() method
        System.out.println(
            "Output for given absolute path : "
            + path1.getPath());
 
        // Creating another object of File and this time
        // relative path is provided as an input
        File path2 = new File("Notes/../demo.txt");
 
        // Print the display the path string for
        // relative path using getPath() method
        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




// Java Program illustrating the getAbsolutePath() Method
 
// Importing all input output classes
import java.io.*;
 
// Class to get getAbsolutePath
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating a file object where
        // relative path is provided as in parameter
        File path = new File("Notes/../demo.txt");
 
        // Print and display the string representing
        // absolute path of the file
        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




// Java Program illustrating the getCanonicalPath() method
 
// Importing all input output classes
import java.io.*;
 
// Class for showcasing getCanonicalPath method
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
        // Creating a new File object and
        // providing it relative path as in arguments
        File path = new File("Notes/../demo.txt");
 
        // Print an display the canonical path
        // name string
        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

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads