Difference between getpath () and getabsolutepath () of file and GetCanonicalPath ()

Source: Internet
Author: User

These methods were accidentally discovered. I didn't know what it meant at the time, so I went to Baidu and found some columns:

Original address http://www.blogjava.net/dreamstone/archive/2007/08/08/134968.html

I. Introduction

getPath()
public String getPath()
Converts an abstract path name to a path name string. The resulting string uses the default name separator to separate names in the name sequence.

Return Value:
String form of this abstract path name



getAbsolutePath()

<pre name="code" class="java">public String getAbsolutePath()


Returns the absolute path name string of the abstract path. 

If the abstract path name is already an absolute path name, the return path name string, which is the same as the getpath () method. If the abstract path name is an empty abstract path name, return the path name string of the current user directory, which is specified by the system property user. dir. Otherwise, analyze the path name in a system-related manner. On UNIX systems, you can analyze a relative path name based on the current user directory to make this path an absolute path name. On Microsoft Windows, you can use the current drive directory specified by the path name (if any) to analyze a relative path name to make it an absolute path name. Otherwise, it can be analyzed based on the current user directory.

Return: absolute path name string, which indicates the same file or directory as this abstract path name. Thrown: securityexception-if the required system attribute value cannot be accessed. For more information, see isabsolute ()

getCanonicalPath()

public String getCanonicalPath()                        throws IOException


Returns the standard path name string of the abstract path.
The standard path name is an absolute path name and is unique. The accurate definition of the standard path name is related to the system. If necessary, this method first converts the path name to an absolute path name, which is the same as calling the getabsolutepath () method, and then maps it to its unique path name in a system-related manner. This usually involves removing redundant names (such ". "and ".. "), analyzes the symbolic connection (for UNIX platforms), and converts the drive name to a standard case (for Microsoft Windows platforms ).

Indicates that each path name of an existing file or directory has a unique standard format. Each path name of a non-existing file or directory also has a unique standard form. The standard form of non-existing file or directory path name may be different from the standard form of the same path name after the file or directory is created. Similarly, the standard form of the path name of an existing file or directory may be different from the standard form of the same path name after the file or directory is deleted.


Return Value:
Indicates the standard path name string of a file or directory with the same abstract path name
Throw:
Ioexception-if an I/O error occurs (it may be because a file system query is required to construct a canonical path name)
Securityexception-The securitymanager. checkread (Java. Io. filedescriptor) method rejects access to the file if the required system attribute value cannot be accessed or the security manager exists.
Start with the following versions:
Jdk1.1

Ii. Example:
1. Difference Between getpath () and getabsolutepath ()

Public static void test1 () {file file1 = new file (". \ test1.txt "); file file2 = new file (" d :\\ workspace \ test \ test1.txt "); system. out. println ("----- default relative path: different paths ------"); system. out. println (file1.getpath (); system. out. println (file1.getabsolutepath (); system. out. println ("----- default absolute path: Get the same path ------"); system. out. println (file2.getpath (); system. out. println (file2.getabsolutepath ());}
Result:

----- Default relative path: the obtained path is different ------. \ test1.txtd: \ workspace \ test \. \ test1.txt ----- default absolute path: the path is the same ------ D: \ workspace \ test \ test1.txtd: \ workspace \ test \ test1.txt
Because getpath () obtains the path when constructing the file.
Getabsolutepath () returns the full path.
If the full path is constructed, the full path is directly returned.
If you try the relative path during the construction, the path of the current directory + the path when constructing the file will be returned.

2. The difference between getabsolutepath () and GetCanonicalPath ()

public static void test2() throws Exception{        File file = new File("..\\src\\test1.txt");        System.out.println(file.getAbsolutePath());        System.out.println(file.getCanonicalPath());    }
Result

D:\workspace\test\..\src\test1.txtD:\workspace\src\test1.txt
We can see that canonicalpath is not only a full path, but also resolves the symbols like.
3. GetCanonicalPath () is different from its own.
Is to explain this paragraph:

Indicates that each path name of an existing file or directory has a unique standard format. Each path name of a non-existing file or directory also has a unique standard form. The standard form of non-existing file or directory path name may be different from the standard form of the same path name after the file or directory is created. Similarly, the standard form of the path name of an existing file or directory may be different from the standard form of the same path name after the file or directory is deleted.
The code below cannot see the result. You need to take some operations into consideration. The following steps are also described:

public static void test3() throws Exception{        File file = new File("D:\\Text.txt");        System.out.println(file.getCanonicalPath());    }
Steps:
Make sure your system is windows.
(1) check that the text.txt file does not exist on the D Drive. directly execute this code and the result is:

D:\Text.txt
Let's try to write text.txt
(2)create a file under the ddrive named text.txt and run the code again. The result is displayed.

D:\text.txt
The same Code gets different results.
At the same time, we can compare it with getabsolutepath (). The result is the same.

Cause:
Windows is not easy to write. In other words, test.txt and test.txt are files on Windows. Therefore, when a file does not exist on Windows, the obtained path is based on the input path. But when the file exists, it will be displayed according to the actual situation. This is why a file is created and deleted. Folder and file are similar.

Iii. Final:
1. Try to execute the above steps in Linux. The result of the two prints is the same, because Linux is a case-sensitive system.
2. manually delete test.txt and then try to execute the following code:

public static void test4() throws Exception{        File file = new File("D:\\Text.txt");        System.out.println(file.getCanonicalPath());        File file1 = new File("D:\\text.txt");        file1.createNewFile();        file = new File("D:\\Text.txt");        System.out.println(file.getCanonicalPath());    }
public static void test3() throws Exception{        File file1 = new File("D:\\text.txt");        file1.createNewFile();        File file = new File("D:\\Text.txt");        System.out.println(file.getCanonicalPath());    }

Execute the above two functions, look at the results, and then think about why?
1. The result is two uppercase letters,
2. Try two lower-case results.
Is there a conflict between two consecutive capital statements and the above statements?
This is caused by the cache mechanism of the virtual machine. The first file = new file ("D: \ text.txt"); determines the result.














Difference between getpath () and getabsolutepath () of file and GetCanonicalPath ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.