Friday, June 9, 2023
HomeJavaDistinction between getPath(), getCanonicalPath() and getAbsolutePath() of File in Java

Distinction between getPath(), getCanonicalPath() and getAbsolutePath() of File in Java

File API is a vital one in Java, it offers entry to the File system to Java packages. Although Java’s file API is wealthy, there are lots of subtleties to know once you use them. One of many widespread question programmer’s has about file path is distinction between getPath(), getCanonicalPath() and getAbsolutePath() strategies, why there are three strategies to get file path and what occurs for those who name getPath() rather than getCanonicalPath(). By the way in which, earlier than understanding the distinction between getPath(), getAbsolutePath() and getCanonicalPath() let’s perceive the idea behind these strategies, i.e. the distinction between path, absolute path, and canonical path.

Typically, a path is a approach to get to a selected file or listing in a file system, it may be absolute (also referred to as a full path) or relative e.g. relative to the present location. Absolute path defines a path from the basis of the file system e.g. C: or D: in Home windows and from / in UNIX based mostly working programs e.g.
Linux or Solaris.

The canonical path is a bit bit difficult as a result of all canonical path is absolute, however vice-versa will not be true. It truly defines a singular absolute path to the file from the basis of the file system. For instance, C://temp/names.txt is a canonical path to names.txt in Home windows, and /dwelling/javinpaul/check/names.txt is a canonical path in Linux.

Then again, there could be many absolute path to the identical file, together with the canonical path which has simply been seen. For instance, one other absolute path to the identical file in Home windows could be C://temp/./names.txt; equally in UNIX /dwelling/javinpaul/check/./names.txt is one other absolute path to the identical file. So you possibly can say that absolutely the path could comprise meta characters like . and .. to signify present and mother or father listing.

In remainder of this text, we are going to study distinction between getPath(), getAbsolutePath() and getCanonical() Path by taking a look at values it return for a selected file.

What’s Absolute, Relative and Canonical Path

You usually heard the time period, absolute, canonical and relative path whereas coping with recordsdata in UNIX, Home windows, Linux or any file system. These are three widespread methods to reference any explicit file in a script or program. 

If you’re a programmer, writing script then you know the way utilizing absolute path could make your script inflexible and in-flexible, infact utilizing absolute path, infamously often called hard-coding path in script is likely one of the unhealthy coding follow in programmer’s dictionary.

 An absolute path is full path to a selected file resembling C:tempabc.txt. The definition of absolute pathname can be system dependent. On UNIX programs, a pathname is absolute if its prefix is “/”. On Win32 programs, a pathname is absolute if its prefix is a drive specifier adopted by “”, or if its prefix is “”.

For instance, we’ve two directories: temp and temp1 and check.txt file is in temp listing.

In Java below Home windows, you could have the next potential absolute paths that check with the identical file check.txt.



Then again, relative path is relative to the listing you’re in, often called present listing. So in case you are within the above listing, then for those who reference file check.txt as relative, it assumes the identical listing you’re in. Whenever you do ../ then it goes again one listing, also referred to as mother or father listing. Canonical paths are a bit tougher. For starters, all canonical paths are absolute (however not all absolute paths are canonical). 

A single file present on a system can have many various paths that check with it, however just one canonical path. Canonical offers a singular absolute path for a given file. The small print of how that is achieved are most likely system-dependent. 

For the above instance, we’ve one and just one canonical path: C:temptest.txt, Keep in mind in Java you possibly can UNIX fashion ahead slash (/) use path separator or you possibly can even get working programs path separator utilizing file.separator system property, a key to put in writing actually platform impartial Java software.

Distinction between getPath(), getAbsolutePath() and getCanonicalPath() in Java

When you perceive distinction between absolute, canonical and relative path, it will be very simple to distinguish between these three methodology, as a result of they really return path, absolute and canonical path. In brief, right here is essential distinction between them :

  1. The primary methodology, getPath()  return a String which denotes the trail that’s used to create related File object, and it might be relative to present listing.
  2. The second methodology, getAbsolutePath() returns the trail string after resolving it in opposition to the present listing if it is relative, leading to a totally certified path.
  3. The third methodology, getCanonicalPath() returns the trail string after resolving any relative path in opposition to present listing, and removes any relative path component e.g. (. and ..), and any file system hyperlinks to return a path which the file system considers the canonical means to reference the file system object to which it factors.

Additionally keep in mind that , every of above two methodology has a File equal which returns the corresponding File object e.g. getAbsoluteFile() and getCanonicalFile() which returns identical factor.

Difference between Path, absolute Path and Relative path java

getPath() vs getAbsolutePath() vs getCanonicalPath()

The next instance reveals how there could be many various paths (and absolute paths) to the identical file, which all have the very same canonical path. Thus canonical path is beneficial if you wish to know if two totally different paths level to the identical file or not.


 * Java program to point out distinction between path, absolute path and canonical
 * path associated to recordsdata in Java. File API gives three strategies to
 * class getPath(), getAbsolutePath() and getCanonicalPath() and
 * this program simply clarify what these methodology returns.
 * @creator Javin Paul
public class PathDemo {

    public static void most important(String args[]) {
        System.out.println("Path of the given file :");
        File youngster = new File(".././Java.txt");

        File mother or father = youngster.getParentFile();
        System.out.println("Path of the mother or father file :");
        displayPath(mother or father);

    public static void displayPath(File testFile) {
        System.out.println("path : " + testFile.getPath());
        System.out.println("absolute path : " + testFile.getAbsolutePath());

        strive {
            System.out.println("canonical path : " 
                      + testFile.getCanonicalPath());
        } catch (Exception e) {


Path of the given file :
path : ...Java.txt
absolute path : C:CustomersWINDOWS 8workspaceDemo...Java.txt
canonical path : C:CustomersWINDOWS 8workspaceJava.txt

Path of the mother or father file :
path : ...
absolute path : C:CustomersWINDOWS 8workspaceDemo...
canonical path : C:CustomersWINDOWS 8workspace

That is all about distinction between getPath(), getAbsolutePath() and getCanonicalPath() in Java. Within the course, we’ve additionally realized distinction between path, absolute path, and canonical path. What you should bear in mind is that getPath() offers you the trail on which File object is created, which can or is probably not relative; getAbsolutePath() offers an absolute path to the file; and getCanonicalPath() offers you the distinctive absolute path to the file. It is price noting that there is usually a big variety of absolute paths that time to the identical file, however just one canonical path.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments