The difference between file GetPath GetAbsolutePath and Getcanonicalpath

Source: Internet
Author: User

Transferred from: http://www.blogjava.net/dreamstone/archive/2007/08/08/134968.html

There are different ways to get path in file, and the following is a detailed distinction.

Conceptual differences: (content from the JDK, the personal feeling of this descriptive information, can only let the understanding of people understand, do not understand the person seems to be a bit difficult (special test Chinese version, English version slightly better) so after the concept I will illustrate. If you feel tired of the concept, skip straight to see examples. It would be better to look at the concept after reading the example.

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

Return:
The string form of this abstract path name

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


Return:
An absolute pathname string that represents the same file or directory as this abstract path name.
Thrown:
SecurityException-If the desired system property value cannot be accessed.
See also:
Isabsolute ()

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

Each path name that represents an existing file or directory has a unique canonical form. Each pathname that represents a non-existent file or directory also has a unique canonical form. The canonical form of a non-existent 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 file or directory is deleted.


Return:
A canonical pathname string that represents the same file or directory as this abstract path name
Thrown:
IOException-If an I/O error occurs (possibly because a file system query is required to construct the canonical pathname)
SecurityException-If the desired system property value cannot be accessed, or if a security manager exists, and its securitymanager.checkread (Java.io.FileDescriptor) method denies read access to the file
Start from the following versions:
JDK1.1


Second, examples:
The difference between 1,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: Get path different------");
System.out.println (File1.getpath ());
System.out.println (File1.getabsolutepath ());
SYSTEM.OUT.PRINTLN ("-----Default absolute path: Get path same------");
System.out.println (File2.getpath ());
System.out.println (File2.getabsolutepath ());

}


The results obtained:

-----Default relative path: Get a different path------
. \test1.txt
D:\workspace\test\.\test1.txt
-----Default absolute path: Gets the same path------
D:\workspace\test\test1.txt
D:\workspace\test\test1.txt

Because GetPath () gets the path when the file is constructed.
GetAbsolutePath () Gets the full path
If the construction is the full path, then the direct return to the full path
If you try to construct a relative path, return the path of the current directory + the path to construct the file

The difference between 2,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 ());
}

The results obtained

D:\workspace\test\. \src\test1.txt
D:\workspace\src\test1.txt

You can see that Canonicalpath is not only the full path, but also the. Or. Such symbols are parsed out.
3,getcanonicalpath () is different from his own.
is to explain this passage:

Each path name that represents an existing file or directory has a unique canonical form. Each pathname that represents a non-existent file or directory also has a unique canonical form. The canonical form of a non-existent 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 file or directory is deleted.

Single bottom This code is not see the results, to cooperate with a certain operation to see. Follow the steps below while explaining

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), determine that the D disk does not Text.txt this file, directly execute this code, the result is:

D:\Text.txt

Pay attention to the Text.txt here.
(2) Create a file under D, called Text.txt, and execute the code again to get the result

D:\text.txt

The same code gets different results.
You can also compare GetAbsolutePath () to see that the result is the same.

Reason:
window is case insensitive, that is, Test.txt and Test.txt are a file on Windows, so when a file does not exist in Windows, the resulting path is followed by the path entered. However, when the file is present, it is displayed in the actual situation. This means that after the file is created and the file is deleted, there are different reasons. Folders and files are similar.

Third, the Last:
1, try to perform the above steps under 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 code below

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 ());
}

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

The difference between file GetPath GetAbsolutePath 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.