Open In App

Difference Between getPath() and getCanonicalPath() in Java

Improve
Improve
Like Article
Like
Save
Share
Report

getPath() method is a part of File class. This function returns the path of the given file object. The function returns a string object which contains the path of the given file object whereas the getCanonicalPath() method is a part of Path class. This function returns the Canonical pathname of the given file object. If the pathname of the file object is Canonical then it simply returns the path of the current file object. The Canonical path is always absolute and unique, the function removes the ‘.’ ‘..’ from the path if present. Now, both methods getPath() and getCanonicalPath() both are part of the File class and both are File methods to get the File system path. First, we understand the difference between both paths from the following file structure.

Illustration: Considering a random path in the windows directory

|--D:
   \--Article      
   \--Test
   \--Program
       \--Python
       \--JAVA
           \Program1.java

If the path- “Python/../JAVA/Program1.java” is passed pass then the value of Path and the canonical path is like the following.

  • Path: Python\..\JAVA\Program1.java
  • Canonical Path: d:\Program\JAVA\Program1.java

Now let’s understand the difference between both methods

(A)Theoretical Differences

getPath() Method getCanonicalPath() Method
This function returns the abstract pathname as a string. This function returns the Canonical pathname of the given file object.
The returned pathname is might be an absolute path. The canonical path is always an absolute and unique path. 
If String pathname is used to create a file object, it simply returns the pathname. This method first converts this pathname to absolute form if needed. To do that it will invoke the getAbsolutePath() Method and then maps it to its unique form.
If a URL is used in the argument of the file object then it removes the protocol and returns the file name  This method removes the redundant names such as ” . “ and  “.. ” from the pathname. It will resolve the symbolic links and convert drive letters to a standard case.
This method is not going to throw any exceptions.

This method throws the following exceptions. 

Security Exception: if the required property value cannot be accessed.

I/O Exception: if I/O exception occurs.

Example:

File f = new File(“c:\\users\\..\\program\\.\\file1.txt”)

Path :- c:\users\..\program\.\file1.txt

Example:

File f = new File(“c:\\users\\..\\program\\.\\file1.txt”)

Canonical path :- c:\program\file1.txt

Implementation: (B) Practical differences

Example

Java




// Java program to demonstrate the difference
// between getPath() and  getCanonicalPath() function
  
// Importing input output classes
import java.io.*;
  
// Class
public class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
        // Try block to check exceptions
        try {
  
            // Creating a file object
            File f = new File(
                "g:\\gfg\\A(7)PATH\\Test\\..\\file1.txt");
  
            // Getting the Path of file object
            String path = f.getPath();
  
            // Getting the Canonical path of the file object
            String canonical = f.getCanonicalPath();
  
            // Display the file path of the file object
            // and also the Canonical path of file
            System.out.println("Path : " + path);
            System.out.println("Canonical path : "
                               + canonical);
        }
  
        // Catch block to handle if exception/s occurs
        catch (Exception e) {
  
            // Exception message is printed where occurred
            System.err.println(e.getMessage());
        }
    }
}


Output:

Path (getPath()): g:\gfg\A(7)PATH\Test\..\file1.txt
path (getCanonicalPath()) : G:\gfg\A(7)PATH\file1.txt


Last Updated : 11 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads