File getCanonicalFile() method in Java with Examples

The getCanonicalFile() method is a part of File class. This function returns the Canonical File of the given file object.If the File path of the file object is Canonical then it simply returns the File of the current file object.
The Canonical File is always absolute and unique, the function removes the ‘.’ ‘..’ from the path of the File, 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 getCanonicalFile() will return a file whose path 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 File getCanonicalFile()

Function Syntax:



file.getCanonicalFile()

Parameters: This function does not accept any parameters.

Return value: This function returns File object, the Canonical File 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 program will illustrate the use of getCanonicalFile() method:

Example 1: Here “program.txt” is a file present in the present working directory

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// use of getCanonicalFile() 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("program.txt");
  
            // Get the Canonical file
            // of the given file f
            File canonical = f.getCanonicalFile();
  
            // 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.getPath());
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}

chevron_right


Output:

Original file path: program.txt
Canonical file path: C:\Users\pc\eclipse-workspace1\arnab\program.txt

Example 2: We are given a File object we have to create the canonical file from that file object.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// use of getCanonicalFile() 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 file
            // of the given file f
            File canonical = f.getCanonicalFile();
  
            // 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.getPath());
        }
        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.