Difference between getPath() and getCononicalPath() in Java
Last Updated :
14 Sep, 2022
Prior to discussing the differences lets quickly recap all three methods
- getPath() Method
- getAbsolutePath() Method
- getCanonicalPath() 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-separator character to separate the names in the name sequence.
Returns: The string form of an abstract pathname
Example
Java
// Java Program illustrating the getPath() method
// Importing input output classes
import java.io.*;
// Class for getPath method()
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a new file object in which input is
// absolute path as in argument from the user
File path1 = new File(
"C:/Users/ASPIRE/Desktop/Java/Notes/Chapter one/demo.txt");
// Print the display the path string for
// absolute path using getPath() method
System.out.println(
"Output for given absolute path : "
+ path1.getPath());
// Creating another object of File and this time
// relative path is provided as an input
File path2 = new File("Notes/../demo.txt");
// Print the display the path string for
// relative path using getPath() method
System.out.println(
"Output for given relative path : "
+ path2.getPath());
}
}
Output:

2. getAbsolutePath() Method
getAbsolutePath() returns a path object representing the absolute path of the given path. If the given pathname is already absolute, then the pathname string is simply returned as if by the getPath() method. If this abstract pathname is the empty abstract pathname then the pathname string of the current user directory, which is named by the system property user '.dir'(), is returned. Otherwise, this pathname is resolved in a system-dependent way.
Returns: The absolute pathname string denoting the same file or directory as this abstract pathname
- On Unix's System: A relative pathname is made absolute by resolving it against the current user directory.
- On Microsoft System: A relative pathname is made absolute by resolving it against the current directory of the drive named by the pathname if any. If not, it is resolved against the current user directory.
Example:
Java
// Java Program illustrating the getAbsolutePath() Method
// Importing all input output classes
import java.io.*;
// Class to get getAbsolutePath
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a file object where
// relative path is provided as in parameter
File path = new File("Notes/../demo.txt");
// Print and display the string representing
// absolute path of the file
System.out.println("Output : "
+ path.getAbsolutePath());
}
}
Output:

3. getCanonicalPath() Method
This method returns the canonical pathname string of the given abstract pathname. A canonical pathname is both absolute and unique. This method first converts the pathname to absolute form if necessary, as if by invoking the getAbsolutePath() method, and then maps it to its unique form in a system-dependent way.
Returns: The canonical pathname string denoting the same file or directory as the abstract pathname.
Java
// Java Program illustrating the getCanonicalPath() method
// Importing all input output classes
import java.io.*;
// Class for showcasing getCanonicalPath method
public class GFG {
// Main driver method
public static void main(String[] args)
throws IOException
{
// Creating a new File object and
// providing it relative path as in arguments
File path = new File("Notes/../demo.txt");
// Print an display the canonical path
// name string
System.out.println("Output: "
+ path.getCanonicalPath());
}
}
Output:

Outputs explanation: In order to interpret better with these CMD outputs or hardcoded outputs the specified location of java files used are as follows
:C:\Users\ASPIRE\Desktop\Java\getPathExample or getAbsoltePathExample or getCanonicalPathExample
Location of demo.txt file
:C:\Users\ASPIRE\Desktop\Java\Notes\Chapter one\demo.txt
Now after discussing each of them lets dive on to differences between getPath(), getAbsolutePath(), getCanonicalPath() which are as follows:
getPath() | getAbsolutePath() | getCononicalPath() |
---|
This method returns a string that denotes (absolute or relative) pathname of the file represented by the file object. | This method returns the absolute pathname string of the abstract file pathname. | This method returns the canonical pathname string of the given abstract pathname. |
If the file object is created using a relative path then the path returned is a relative path. | If the abstract pathname is relative, then it is resolved in a system-dependent way. | If the file object is created using a relative path then this method first converts pathname to absolute and maps it to a unique form. |
If the file object is created using the absolute path then the path returned is the absolute path. | If the abstract pathname is already absolute, then the same path name string is returned. | If the file object is created using a relative path then the path returned will be in unique form. |
This method does not resolve pathname. | This method only resolves the current directory for a relative path. Shorthand representations (such as “.” and “..”) are not resolved further. | This method involves removing redundant names such as “.” and “..” from the pathname, resolving symbolic links (On Unix platform), and converting drive letters to a standard case (On Microsoft Windows platform). |
Example
On window's System
File path= new File(“Notes/../demo.txt");
Output:
Notes\..\demo.txt
On Unix's System
File path = new File("Notes/../demo.txt ")
Output:
Notes/../demo.txt
|
Example
Window's System
File path= new File(“Notes/../demo.txt”);
Output:
C:\Users\ASPIRE\Desktop\Java\Notes\..\demo.txt
On Unix's System
File path = new File(“Notes/../demo.txt “)
Output:
home/pooja/Desktop/Notes/../demo.txt
|
Example
On Window's System
File path= new File(“Notes/../demo.txt”);
Output:
C:\Users\ASPIRE\Desktop\Java\demo.txt
On Unix's System
File path = new File("Notes/../demo.txt “)
Output:
/home/pooja/Desktop/Java/demo.txt
|
Similar Reads
Difference Between getCanonicalPath() and getAbsolutePath() in Java
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 pa
5 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 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 JDK and JRE in Java
JDK and JRE are the core concepts in Java programming and their differences are some of the most popular interview questions. We don't use these concepts while programming but if we want to become a Java developer, we must know about these concepts. JDKJDK stands for Java Development Kit. It is a so
2 min read
Difference between static and non-static variables in Java
There are three types of variables in Java: Local VariablesInstance VariablesStatic Variables The Local variables and Instance variables are together called Non-Static variables. Hence it can also be said that the Java variables can be divided into 2 categories: Static Variables: When a variable is
4 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
Differences Between JDK, JRE and JVM
Understanding the difference between JDK, JRE, and JVM plays a very important role in understanding how Java works and how each component contributes to the development and execution of Java applications. The main difference between JDK, JRE, and JVM is:JDK: Java Development Kit is a software develo
3 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