What is the difference between the file class GetPath (), GetAbsolutePath (), Getcanonicalpath () in Java?

Source: Internet
Author: User

A simple look at the description, the example is the most important.

1, GetPath ():

Returns the path to the definition, (that is, what path you write, and what path he returns)

2, GetAbsolutePath ():

  Returns an absolute path, but does not process "." and ".." The situation

3, Getcanonicalpath ():

Returns the normalized absolute path, which is equivalent to the "." In GetAbsolutePath (). and ".." Parse the correct path to the corresponding

First example: (using: ". \\src\\test.txt" a point path)

  File File = new file (". \\src\\test.txt");    System.out.println (File.getpath ());    System.out.println (File.getabsolutepath ());    System.out.println (File.getcanonicalpath ());    Results:. \src\test.txtd:\workspace\java_io\.\src\test.txtd:\workspace\java_io\src\test.txt

The second example: (using: ". \\src\\test.txt "Two-point path)

File File = new file (".. \\src\\test.txt "); System.out.println (File.getpath ()); System.out.println (File.getabsolutepath ()); System.out.println (File.getcanonicalpath ()); Results: \src\test.txtd:\workspace\java_io\. \src\test.txtd:\workspace\src\test.txt  Note that the path to the result is not the same as a point, because he resolves the "." and ".." The situation.

A third example: (using: "D:\\test\\absolutetest.txt" absolute path)

File File = new file ("D:\\test\\absolutetest.txt"); System.out.println (File.getpath ()); System.out.println (File.getabsolutepath ()); System.out.println (File.getcanonicalpath ()); Results: d:\test\absolutetest.txtd:\test\absolutetest.txtd:\test\ AbsoluteTest.txt

Conclusion:

1. When the input is an absolute path, the absolute path is returned.

2. When the input is a relative path:

2.1, GetPath () return is the path in the file construction method, what is what, does not increase not minus

2.2, GetAbsolutePath () returned is actually the content of User.dir+getpath (), from the above view: D:\workspace\java_io\.\src\test.txt,d:\workspace\java_io \.. \src\test.txt, can be drawn.

2.3. Getcanonicalpath () returns the standard path to fully parse the symbol

What is the difference between the file class GetPath (), GetAbsolutePath (), Getcanonicalpath () in Java?

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.