Open In App

File getCanonicalPath() method in Java with Examples

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The getCanonicalPath() method is a part of the 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.

For Example:

if we create a file object using the path as “program.txt”, it points to the file present in the same directory where the executable program is kept (if you are using an IDE it will point to the file where you have saved the program ). Here the path of the file mentioned above is “program.txt” but this path is not absolute (i.e. not complete). The function getCanonicalPath() will return a path that will be an absolute and unique path from the root directories. The canonical form of an existing file may be different from the canonical form of the same non-existing file and the canonical form of an existing file may be different from the canonical form of the same file when it is deleted.

Function Signature:

public String getCanonicalPath()

Function Syntax:

file.getCanonicalPath()

Parameters: This function does not accept any parameters.

Return value: The function returns a String value if the Canonical Path of the given File object.

Exception: This method throws the following exceptions:

  • Security Exception if the required property value cannot be accessed.
  • I/O Exception if I/O exception occurs.
  • The below programs will illustrate the use of getAbsolutePath() method:

    Example 1:

    We have a File object with a specified path we will try to find its canonical path.

    Java

    
    // Java program to demonstrate the
    // use of getCanonicalPath() function
    
    import java.io.*;
    
    public class solution {
        public static void main(String args[])
        {
            // try-catch block to handle exceptions
            try {
    
                // Create a file object
                File f = new File("c:\\program");
    
                // Get the Canonical path of file f
                String canonical = f.getCanonicalPath();
    
                // Display the file path of the file object
                // and also the file path of Canonical file
                System.out.println("Original file path : "
                                   + f.getPath());
                System.out.println("Canonical file path : "
                                   + canonical);
            }
            catch (Exception e) {
                System.err.println(e.getMessage());
            }
        }
    }
    

    Output

    Original file path : c:\program
    Canonical file path : C:\program

    Example 2:

    We have a File object with a specified path we will try to find its canonical path .

    Java

    
    // Java program to demonstrate the
    // use of getCanonicalPath() function
    
    import java.io.*;
    public class solution {
        public static void main(String args[])
        {
            // try-catch block to handle exceptions
            try {
    
                // Create a file object
                File f = new File("c:\\users\\..\\program");
    
                // Get the Canonical path of file f
                String canonical = f.getCanonicalPath();
    
                // Display the file path of the file object
                // and also the file path of Canonical file
                System.out.println("Original file path : "
                                   + f.getPath());
                System.out.println("Canonical file path : "
                                   + canonical);
            }
            catch (Exception e) {
                System.err.println(e.getMessage());
            }
        }
    }
    

    Output

    Original file path : c:\users\..\program
    Canonical file path : C:\program

    The programs might not run in an online IDE. please use an offline IDE and set the path of the file


    Like Article
    Suggest improvement
    Previous
    Next
    Share your thoughts in the comments

    Similar Reads