文件路径安全性:getPath(),getAbsolutePath(),getCanonicalPath()

java.io.File 类提供3个方法用于获取文件路径:getPath()getAbsolutePath()getCanonicalPath(),这3个方法间的区别可参考以下示例:

@Test
public void test() throws IOException {
    File file = new File("./test.txt");
    System.out.println(file.getPath());
    System.out.println(file.getAbsolutePath());
    System.out.println(file.getCanonicalPath());
}

运行结果:

.\test.txt
D:\Tutorial\java-tutorial\.\test.txt
D:\Tutorial\java-tutorial\test.txt
  • getPath() 直接返回构造 java.io.File 实例时使用的路径参数
  • getAbsolutePath() 返回绝对路径,但不会处理相对路径符号 [.] 和 [..]
  • getCanonicalPath() 返回规范的绝对路径,即是绝对也是唯一

Java API:

public String getPath()

Converts this abstract pathname into a pathname string. The resulting string uses the default name-separator character to separate the names in the name sequence.

Returns:
The string form of this abstract pathname
public String getAbsolutePath()

Returns the absolute pathname string of this abstract pathname.
If this abstract pathname is already absolute, then the pathname string is simply returned as if by the getPath() method. 
If this abstract pathname is the empty abstract pathname then the pathname string of the current user directory, which is named by the system property user.dir, is returned. 
Otherwise this pathname is resolved in a system-dependent way. 
On UNIX systems, a relative pathname is made absolute by resolving it against the current user directory. 
On Microsoft Windows systems, a relative pathname is made absolute by resolving it against the current directory of the drive named by the pathname, if any; if not, it is resolved against the current user directory.

Returns:
The absolute pathname string denoting the same file or directory as this abstract pathname

Throws:
SecurityException - If a required system property value cannot be accessed.

See Also:
isAbsolute()
public String getCanonicalPath() throws IOException

Returns the canonical pathname string of this abstract pathname.
A canonical pathname is both absolute and unique. 
The precise definition of canonical form is system-dependent. 
This method first converts this pathname to absolute form if necessary, as if by invoking the getAbsolutePath() method, and then maps it to its unique form in a system-dependent way. 
This typically involves removing redundant names such as "." and ".." from the pathname, resolving symbolic links (on UNIX platforms), and converting drive letters to a standard case (on Microsoft Windows platforms).
Every pathname that denotes an existing file or directory has a unique canonical form. 
Every pathname that denotes a nonexistent file or directory also has a unique canonical form. 
The canonical form of the pathname of a nonexistent file or directory may be different from the canonical form of the same pathname after the file or directory is created. 
Similarly, the canonical form of the pathname of an existing file or directory may be different from the canonical form of the same pathname after the file or directory is deleted.

Returns:
The canonical pathname string denoting the same file or directory as this abstract pathname

Throws:
IOException - If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries
SecurityException - If a required 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

Since:
JDK1.1

See Also:
Path.toRealPath(java.nio.file.LinkOption...)

你可能感兴趣的:(文件路径安全性:getPath(),getAbsolutePath(),getCanonicalPath())