Difference Between getCanonicalPath() and getAbsolutePath() in Java
Last Updated :
03 Jun, 2022
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.
Let us see the differences in a tabular form -:
| getCanonicalPath() | getAbsolutePath() |
1. | It is a Java method present in java.io File class. | It is a Java method present in java.io File class. |
2. |
Its syntax is -:
public String getCanonicalPath() throws IOException
|
Its syntax is -:
public String getAbsolutePath()
|
3. | It does not take any arguments. | It does not take any arguments. |
4. | It returns the canonical pathname string denoting the same file or directory as this abstract pathname. | It returns a String that denotes the absolute pathname string denoting the same file or directory as this abstract pathname. |
5. | It is compatible with java 1.1 + | It is compatible with java 1.0 + |
Similar Reads
Difference between getPath() and getCononicalPath() in Java
Prior to discussing the differences lets quickly recap all three methods getPath() MethodgetAbsolutePath() MethodgetCanonicalPath() Method 1. getPath() Method getPath() is a method of URL class.It converts the given abstract pathname into a pathname string. The resulting string uses the default name
6 min read
Difference Between FileInputStream and FileReader in Java
Let us first do discuss them in order to get the understanding alongside an example to interpret the differences. Here first we will be discussing out FileReader class. So starting of with FileReader class in java is used to read data from the file. It returns data in byte format like FileInputStrea
4 min read
Difference between static and non-static method in Java
A static method is a method that belongs to a class, but it does not belong to an instance of that class and this method can be called without the instance or object of that class. Every method in java defaults to a non-static method without static keyword preceding it. Non-static methods can access
6 min read
Difference between Early and Late Binding in Java
Early Binding: The binding which can be resolved at compile time by the compiler is known as static or early binding. Binding of all the static, private and final methods is done at compile-time. Example: Java public class NewClass { public static class superclass { static void print() { System.out.
2 min read
Difference Between URL and URN in Java
URL stands for Uniform Resource Location. A URL contains a protocol, server name or IP Address, Port Number, filename, or directory name. URL is a subset of URI as perceived from the image shown below that describes the network address or location where the source is available. URL begins with the n
3 min read
Difference Between URI and URN in Java
URI stands for Uniform Resource Identifier. It identifies the resource either by name, or by location, or by both. It allows uniform identification of the resources. Here the resource can be anything like documents, images, files, web pages, etc. that can be part of the web architecture. The identif
2 min read
Difference between File and Folder
File: A File is defined as a set of related data or information that is being stored in secondary storage. A file is data file or a program file where former contains data and information in the form of alphanumeric, numeric or binary and latter containing the program code and can also be executed,
2 min read
Difference between __dirname and ./ in Node.js
Working with any technology requires to interact with files and directories. Files and directories maintain a tree structure for easy access. Working with Node.js also requires accessing files using the file path which can be obtained using different commands. There are two ways of getting the curre
3 min read
Difference Between Path and ClassPath in Java
The ClassPath is a parameter in the Java Virtual Machine(JVM) or the Java compiler that is used by a system or application ClassLoader to locate and load compiled Java bytecodes stored in the ".class" file. On the other hand, The Path is also an environment variable path that behaves as a mediator b
2 min read
Difference Between URL, URI and URN in Java
URI stands for Uniform Resource Identifier. URI is a sequence of characters used to identify a resource location or a name or both over the World Wide Web. A URI can be further classified as a locator, a name, or both.Syntax of URI: Starts with a scheme followed by a colon character, and then by a s
2 min read