Difference Between getPath() and getAbsolutePath() in Java - GeeksforGeeks

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
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.
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.                                                                                                  
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.
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
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
Java-File Class
Picked
Difference Between
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Difference between Prim's and Kruskal's algorithm for MST
Difference Between Spark DataFrame and Pandas DataFrame
Difference between Internal and External fragmentation
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Arrays.sort() in Java with examples
Reverse a string in Java