📜  JavagetPath() 和 getAbsolutePath() 的区别

📅  最后修改于: 2021-09-14 02:45:53             🧑  作者: Mango

getPath() getPath() 方法是 File 类的一部分。此函数返回给定文件对象的路径。该函数返回一个字符串对象,其中包含给定文件对象的路径。

退货类型:

The string form of an abstract pathname

getAbsolutePath() getAbsolutePath() 返回一个路径对象,表示给定路径的绝对路径。如果给定的路径名已经是绝对的,那么路径名字符串将像 getPath() 方法一样简单地返回。如果当前抽象路径名是空抽象路径名,则返回当前用户目录的路径名字符串(由系统属性命名)。否则,此路径名以依赖于系统的方式解析。

在 Unix 系统上:

在微软系统上:

返回:

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