The difference between the GetPath GetAbsolutePath and Getcanonicalpath of file

Source: Internet
Author: User

These few ways to get path are different in the file, so the detailed difference is below.

Conceptual differences: (content from the JDK, the personal feeling of this descriptive information, only to let the understanding of the people understand that the people do not understand it seems to be a bit difficult (especially Chinese version, English version slightly better) so after the concept I will illustrate. If you feel tired to see the concept, skip directly to the example. It would be better to see the concept when you read the example.

GetPath
The public string GetPath () converts this abstract path name to a pathname string. The resulting string uses the default name separator to separate the names in the name sequence.

Return:
The string form of this abstract path name


GetAbsolutePath
The public string GetAbsolutePath () returns an absolute pathname string for an abstract pathname.
If this abstract path name is already an absolute pathname, the pathname string is returned, as in the GetPath () method. If this abstract path name is an empty abstract pathname, the path name string for the current user directory is returned, which is specified by the system property User.dir. Otherwise, the pathname is parsed using a system-related approach. On a UNIX system, you can make the path name an absolute pathname by parsing a relative pathname based on the current user directory. On a Microsoft Windows system, a relative pathname is parsed by the current drive directory (if any) specified by the path name, so that the pathname becomes an absolute pathname, otherwise it can be parsed according to the current user directory.


Return:
An absolute pathname string that represents the same file or directory as this abstract path name.
Thrown:
SecurityException-If you cannot access the required system property values.
See also:
Isabsolute ()


Getcanonicalpath
Public String Getcanonicalpath ()
Throws IOException returns the canonical path name string for the abstract pathname.
The canonical path name is an absolute pathname and is unique. The exact definition of the canonical pathname is related to the system. If necessary, this method first converts the pathname to an absolute pathname, just like the effect of invoking the GetAbsolutePath () method, and then maps it to its unique pathname in a system-related manner. This usually involves removing unnecessary names from the pathname (for example, "." and "..."). , parse symbolic connections (for UNIX platforms), and convert drive names to standard case (for Microsoft Windows platforms).

Indicates that each pathname of an existing file or directory has a unique canonical form. Indicates that each pathname of a file or directory that does not exist also has a unique canonical form. A canonical form that does not exist for a file or directory pathname may be different from the canonical form of the same pathname after the file or directory was created. Similarly, the canonical form of an existing file or directory pathname may be different from the canonical form of the same pathname after the deletion of the file or directory.


Return:
Represents a canonical path name string for a file or directory that is the same as this abstract path name
Thrown:
IOException-If an I/O error occurs (possibly because the constructor path name requires a file system query)
SecurityException-If the desired system property value is not accessible, or if there is a security manager, and its securitymanager.checkread (Java.io.FileDescriptor) method denies read access to the file
Start with the following version:
JDK1.1


Second, examples:
The difference between 1,getpath () and GetAbsolutePath ()
Java code   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: Get a different path------ ");            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 ());                    }  


Results: Java code-----Default relative path: Get a different path------. \test1.txt D:\workspace\test\.\test1.txt-----Default absolute path: Get the same path------D:\work Space\test\test1.txt D:\workspace\test\test1.txt


Because GetPath () gets the path to construct the file.
GetAbsolutePath () Gets the full path
If the construction is the full path, then return the full path directly.
If you are constructing a relative path, return the path to the current directory + the path to construct file

Different Java code public static void Test2 () of 2,getabsolutepath () and Getcanonicalpath () throws exception{File File = new File ("..            \\src\\test1.txt ");            System.out.println (File.getabsolutepath ());        System.out.println (File.getcanonicalpath ()); }


The resulting Java code D:\workspace\test\ ... \src\test1.txt D:\workspace\src\test1.txt


You can see that Canonicalpath is not only a full path, but also. Or. Such symbols are parsed.
3,getcanonicalpath () is different from yourself.
is to explain the passage:
indicates that each pathname of an existing file or directory has a unique canonical form. Indicates that each pathname of a file or directory that does not exist also has a unique canonical form. A canonical form that does not exist for a file or directory pathname may be different from the canonical form of the same pathname after the file or directory was created. Similarly, the canonical form of an existing file or directory pathname may be different from the canonical form of the same pathname after the deletion of the file or directory.

The bottom of this code is not to see the results, to match a certain operation to see. The following steps, while explaining
Java code public static void Test3 () throws exception{File File = new file ("D:\\text.txt");        System.out.println (File.getcanonicalpath ()); }



Steps:
Make sure your system is a Windows system.
(1) To make sure that the file is not Text.txt under D disk and execute the code directly, the result is:
D:\Text.txt, look at the Text.txt here.
(2) Create a file under D disk named Text.txt, execute code again, get the result
D:\text.txt the same code to get different results.
At the same time you can compare GetAbsolutePath () to see, the result is the same.

Reason:
Windows is case insensitive, that is, Test.txt and Test.txt are a file on Windows, so the path that is followed is that of a file that is not present in the window. However, when the file exists, it will be displayed in the actual situation. This is the reason for different files after the file is created and deleted. Folders and files are similar.

Third, finally:
1, try to perform the above steps in Linux, two times the result of printing is the same, because Linux is a case sensitive system.
2, manually delete the test.txt, and then try to execute the bottom code Java 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 =&nbSp;new file ("D:\\text.txt");             File1.createnewfile ();            file file =  new file ("D:\\text.txt");             System.out.println (File.getcanonicalpath ());        }  



Perform the top two functions, see the results, and think about why.
1, the result is two uppercase,
2, the results try two lowercase
Two consecutive capitals, whether with the above contradictions.
This is due to the caching mechanism of the virtual machine. The first time file File = new file ("D:\\text.txt"), the result is determined.

Turn from: http://harbey.iteye.com/blog/489406

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.