Skip to content
Related Articles

Related Articles

Improve Article

Difference Between getCanonicalPath() and getAbsolutePath() in Java

  • Last Updated : 16 May, 2021

The getCanonicalPath() and getAbsolutePath() methods belong to java.io.File class in Java. And these methods are basically used to get the path of a file object in the system’s directory structure.  

AbsoluteFilePath is the pathname of a file object 

  • If we create the file object using an abstract path, then the absolute file path is the same as the abstract file path.
  • If we create the file object using a relative path, then the absolute file path is the path we get after resolving the relative path against the current directory.

CanonicalFilePath is the pathname of a file object 

  • If we create the file object using an abstract path, the canonical file path is the same as the abstract file path
  • If we create the file object using a relative path, the canonical file path is the path that is both the shortest absolute and unique path

Let’s take an example. Say, we create a file object using path “C:/folder1/folder2/folder3/file.txt“. Obviously, the mentioned path is an abstract path, so the absolute file path, as well as the canonical file path, will be the same as above. 

But if the file path mentioned during file object creation is like “C:/folder1/folder2/folder3/folder4/../../folder3/file.txt“, then the absolute file path will be the same as the mentioned path, but the canonical file path will be the shortest absolute path, i.e. “C:/folder1/folder2/folder3/file.txt”.



Method 1: getAbsolutePath()

public String getAbsolutePath() 

Method 2: getCanonicalPath()

public String getCanonicalPath() throws IOException

Example:

Method 1: getAbsolutePath()

file.getAbsolutePath()    
// It returns absolute path of file object in system's directory structure

Method 2: getCanonicalPath()

file.getCanonicalPath()    
// It returns canonical path of file object in system's directory structure

Example 1:

Java




// Java Program to illustrate Difference Between
// getCanonicalPath() and getAbsolutePath()
  
// Importing input output classes
import java.io.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
  
        // Path of Test.txt is E:\workspace\gfg\Test.txt
        // Test.txt is created inside project directory,
  
        // Here project name is gfg
        File file1 = new File("Test.txt");
  
        // Getting th absolute path of the file
        // using getAbsolutePath() method
        System.out.println("Absolute Path: "
                           + file1.getAbsolutePath());
  
        // Getting the canonical path of the file
        // using getCanonicalPath() method
        System.out.println("Canonical Path: "
                           + file1.getCanonicalPath());
    }
}

Output:



Absolute Path: E:\workspace\gfg\Test.txt
Canonical Path: E:\workspace\gfg\Test.txt

Output explanation: Here since the file object has been created with an abstract and full file path, so both methods provide the same output that we have used during file object creation.

Example 2:

Java




// Java Program to illustrate Difference Between
// getCanonicalPath() and getAbsolutePath()
  
// Importing input output classes
import java.io.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
  
        // Creating an object of File class where
        // Path of Test.txt is E:/workspace/gfg/Test.txt
        File file2 = new File(
            "e:\\workspace\\gfg\\..\\gfg\\Test.txt");
  
        // Getting the absolute path of file
        // using getAbsolutePath() method
        System.out.println("Absolute Path: "
                           + file2.getAbsolutePath());
  
        // Getting the canonical path of file
        // using getCanonicalPath() method
        System.out.println("Canonical Path: "
                           + file2.getCanonicalPath());
    }
}

Output:

Absolute Path: e:\workspace_2021-Apr\gfg\..\gfg\Test.txt
Canonical Path: E:\workspace_2021-Apr\gfg\Test.txt

Output explanation:

Here file object has been created using the relative file path, which basically pointing to E:/workspace/gfg/Test.txt. So, while calling getAbsolutePath(), it provides a relative path that mentioned during the creation of the file object. But while calling getCanonicalPath(), it provides an abstract path or direct path and converts the drive letter to a standard case.

Example 3:

Java




// Java Program to illustrate Difference Between
// getCanonicalPath() and getAbsolutePath()
  
// Importing input output classes
import java.io.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
  
        // Now creating File class object where
        // Path of Test.txt is E:/Test.txt
        // Current Directory: E:/workspace/gfg/src
        File file3 = new File("../../Test.txt");
  
        // Getting the absolute path of file
        // using getAbsolutePath()
        System.out.println("Absolute Path: "
                           + file3.getAbsolutePath());
  
        // Getting the canonical path of file
        // using getCanonicalPath()
        System.out.println("Canonical Path: "
                           + file3.getCanonicalPath());
    }
}

Output:

Absolute Path: E:\workspace\gfg\..\..\Test.txt
Canonical Path: E:\Test.txt

Output Explanation:

Here file object has been created with a complete relative path. And while calling getAbsolutePath(), we can see how we get the path, not in a fully relative one but, partial relative path. But while calling getCanonicalPath(), we can see the direct abstract path in the file system.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :