File getCanonicalPath() method in Java with Examples

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.

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 which 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 a 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 following exceptions:

  • Security Exception if the required property value cannot be accessed.
  • I/O Exception if I/O exception occurs.
  • 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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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());
            }
        }
    }

    chevron_right

    
    

    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 .

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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());
            }
        }
    }

    chevron_right

    
    

    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



    My Personal Notes arrow_drop_up

    Second year Department of Information Technology Jadavpur University

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.