getPath() : getPath() 方法是 File 类的一部分。此函数返回给定文件对象的路径。该函数返回一个字符串对象,其中包含给定文件对象的路径。
退货类型:
The string form of an abstract pathname
getAbsolutePath() : getAbsolutePath() 返回一个路径对象,表示给定路径的绝对路径。如果给定的路径名已经是绝对的,那么路径名字符串将像 getPath() 方法一样简单地返回。如果当前抽象路径名是空抽象路径名,则返回当前用户目录的路径名字符串(由系统属性命名)。否则,此路径名以依赖于系统的方式解析。
在 Unix 系统上:
A relative pathname is made absolute by resolving it against the current user directory.
在微软系统上:
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.
返回:
The absolute pathname string denoting the same file or directory as this abstract pathname
getPath() 和 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 |