Skip to content
Related Articles

Related Articles

Difference Between getPath() and getAbsolutePath() in Java
  • Last Updated : 04 Jan, 2021

getPath(): The getPath() method is a part of File class. This function returns the path of the given file object. The function returns a string object which contains the path of the given file object.

Return Type:

The string form of an abstract pathname

getAbsolutePath(): The getAbsolutePath() returns a path object representing the absolute path of given path. If the given pathname is already absolute, then the pathname string is simply returned as if by the getPath() method. If the current abstract pathname is the empty abstract pathname then the pathname string of the current user directory(named by the system property) is returned. Else, this pathname is resolved in a system-dependent way.

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, it is resolved against the current user directory.

Returns:

The absolute pathname string denoting the same file or directory as this abstract pathname

Difference between getPath() and getAbsolutePath()

                        getPath()                                           getAbsolutePath()                          
1

This method returns a string which denotes the (absolute or relative) pathname of the file represented by the file object.               

This method returns the absolute pathname string of abstract file pathname.

2

If the file object is created using an absolute path then the path returned is an absolute path.



If the abstract pathname is already absolute, then the same pathname string is returned.                                                                                                  

3

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.

4

Example(On Window’s System):

If the absolute path is provided:

File path1 = new File(“C:\\Users\\ASPIRE\\Desktop\\Java folder\\demo.txt”);

Output:

C:\Users\ASPIRE\Desktop\Java folder\demo.txt

If relative path is provided:

File path2 = new File(“..\\demo.txt”);

Output:

..\demo.txt

Example(On Window’s System):

If absolute path is provided:

File path1 = new File(“C:\\Users\\ASPIRE\\Desktop\\Java folder\\demo.txt”);

Output:

C:\Users\ASPIRE\Desktop\Java folder\demo.txt

If relative path is provided:

File path2 = new File(“..\\demo.txt”);

Output:

C:\Users\ASPIRE\Desktop\Java folder\..\demo.txt

5

Example(On Unix’s System):

If absolute path is provided:

File path1 = new File(“home/Pooja/Desktop/Java folder/demo.txt”);

Output:

home/Pooja/Desktop/Java folder/demo.txt

If relative path is provided:

File path2 = new File(“../demo.txt”);

Output:

../demo.txt

Example(On Unix’s System):

If absolute path is provided:

File path1 = new File(“home/Pooja/Desktop/Java folder/demo.txt”);

Output:

home/Pooja/Desktop/Java folder/demo.txt

If relative path is provided:

a)File path2 = new File(“../demo.txt”);

Output:

../demo.txt

b)File path2 = new File(“../Document/abc.txt”);

Output:

/home/pooja/Document/abc.txt

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :