Difference between getPath(), getAbsolute() and getCanonical() file path methods in java | Relative and Absolute path



Difference between getPath(), getAbsolute() and getCanonical() file path methods in java>



First quickly let’s create a small program which will help us in giving best understanding of difference between getPath(), getAbsolute() and getCanonical() file path methods in java.



import java.io.File;
import java.io.IOException;
public class PathTesting {
   public static void main(String[] args) throws IOException {
          File f = new File("myPath/./../myFile.txt");
          System.out.println(f.createNewFile() ? "file created"
                       : "file not created");
          System.out.println("----");
          System.out.println("getPath()       > "+ f.getPath());
          System.out.println("getAbsolutePath()  > "+ f.getAbsolutePath());
          System.out.println("getCanonicalPath() > "+ f.getCanonicalPath());
   }
}
/*OUTPUT
file created
----
getPath()          > myPath\.\..\myFile.txt
getAbsolutePath()  > E:\workspace\FileHandling\myPath\.\..\myFile.txt
getCanonicalPath() > E:\workspace\FileHandling\myFile.txt
*/
After executing above program E:\workspace\FileHandling\myFile.txt will be created.


Now, lets discuss these getPath(), getAbsolute() and getCanonical() file path methods >

getPath() - getPath() method returns the path with which the File object was constructed.
path returned by getPath() may or may not be relative.

Example -
In the above program getPath() returned myPath\.\..\myFile.txt i.e. the path with which File object was constructed.


getAbsolutePath() - getAbsolutePath() method returns the path with which the File object was constructed but also includes information about the current directory if the path is relative.

There may be more than one absolute path to file,  but there is always one canonical path to file.

Example -
In the above program getAbsolutePath() returned E:\workspace\FileHandling\myPath\.\..\myFile.txt i.e. the path with which the File object was constructed but also includes current directory information if the path is relative.
Here E:\workspace\FileHandling\ is the current directory information.


getCanonicalPath() - getCanonicalPath() method returns unique absolute path to the file.
  • Method removes all references like (/. and /..), if any.
  • Method throws IOException if file system operation fails.
  • A canonical path is always an absolute path, but vice-versa is not true. Please see below table for elaborated information.
  • There is always one canonical path to file.

Example -
In the above program getCanonicalPath() returned E:\workspace\FileHandling\myFile.txt i.e. the unique absolute path to the file. Method removes all references like (\myPath\.\..)


Let’s take more intense look at file path we have used in above program >


File path
Path
Absolute path
Canonical path
E:\workspace\FileHandling\myFile.txt
Yes
Yes
Yes
\myPath\.\..
Yes
No
No
E:\workspace\FileHandling\myPath\.\..\myFile.txt
Yes
Yes
No
E:\workspace\FileHandling\myFile.txt is a path, absolute path and canonical path.
\myPath\.\.. is a path but its not an absolute path nor a canonical path.
E:\workspace\FileHandling\myPath\.\..\myFile.txt is a path and absolute path but its not a canonical path.




Difference between Relative and Absolute file path >

Relative path depends on your current working directory directory.

Absolute path does not depends on your current working directory directory. It’s complete path of file.

Let’s say we have such directory structure in windows >

Example 1 >
Current working directory is C:\ and we want to go to C:\a

Relative path way -
C:\> cd a
C:\a>

Absolute path way (type complete path cd C:\a)-
C:\> cd c:\a
C:\a>

Example 2 >
Current working directory is C:\a and we want to go to C:\

Relative path way -
C:\a> cd..
C:\>

Absolute path way (type complete path) -
C:\a> cd C:\
C:\>

Example 3 >
Current working directory is C:\ and we want to go to C:\a\b2\c

Relative path way -
C:\> cd a\b2\c
C:\a\b2\c>

Absolute path way (type complete path)-
C:\> cd C:\a\b2\c
C:\a\b2\c>


Example 4 >
Current working directory is C:\a\b2\c and we want to go to C:\a\b1

Relative path way -
C:\a\b2\c> cd..\..\b1
C:\a\b1>

Absolute path way (type complete path)-
C:\a\b2\c> cd C:\a\b1
C:\a\b1>






RELATED LINKS>

Find current working directory or current path in java

Difference between loading file with FileInputStream and getResourceAsStream() in java

Download image from specified URL in java


Labels: Core Java
eEdit
Must read for you :