<title>There are different ways to get path in file, and the following is a detailed distinction.</title> 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 Properties User.dir. Otherwise, this pathname is parsed using a system-related approach. In? UNIX? On the system, you can make the path name an absolute pathname by parsing a relative pathname based on the current user directory. In? Microsoft? On a 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 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 the desired system property value cannot be accessed.
See also:
Isabsolute ()
Getcanonicalpath
Public? String?getcanonicalpath ()
????????????????????????throws? IOException returns the canonical pathname string for the abstract path name.
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 with the effect of calling the? GetAbsolutePath () method, and then maps it to its unique path name in a system-dependent way. This usually involves removing redundant names from the pathname (for example, "."? and? "..." ), analyze symbolic connections (for? UNIX platform), and convert drive names to standard case (for? Microsoft? Windows? platform).?
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 pathname?
Thrown:?
IOException?-? If it happens? I/O error (possibly because a file system query is required to construct the canonical pathname)?
SecurityException?-? If the desired system property value cannot be accessed, or a security manager exists, and its? Securitymanager.checkread (java.io.FileDescriptor)? method denies read access to the file
Starting 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 to the time the file was 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 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 notice here the Text.txt of the capitalization
(2) Create a file under D, called Text.txt, and execute the code again to get the result
D:\text.txt the same code to get 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. First time file?file?=?New? File ("D:\\text.txt"); The result is determined.
From for notes (Wiz)
There are different ways to get path in file, and the following is a detailed distinction.