Difference Between getPath() and getCanonicalPath() in Java - GeeksforGeeks

02 Dec, 2021
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
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 classesimport java.io.*; // Classpublic 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
sumitgumber28
varshagumber28
Java-File Class
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?
Program to print ASCII Value of a character