首先介绍下我们今天的三个主角:
- getPath
- getAbsolutePath
- getCanonicalPath
先上代码:
package test;
import java.io.File;
import java.io.IOException;
public class Test2 {
public static void main(String[] args) throws IOException {
File file=new File("../readme.txt");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
System.out.println(file.getCanonicalPath());
}
}
运行结果:
../readme.txt
/home/dusk/我就是我/Code/Workspaces/MyEclipse for Spring 2014/test/../readme.txt
/home/dusk/我就是我/Code/Workspaces/MyEclipse for Spring 2014/readme.txt
先说getPath
/**
* Converts this abstract pathname into a pathname string. The resulting
* string uses the {@link #separator default name-separator character} to
* separate the names in the name sequence.
* <p>
* 将此抽象路径名转换为一个路径名字符串。所得到的字符串使用默认名称分隔符来分隔名称序列中的名称。
* @return The string form of this abstract pathname
*/
public String getPath() {
return path;
}
再说
getAbsolutePath
/**
* Returns the absolute pathname string of this abstract pathname.
*
* <p> If this abstract pathname is already absolute, then the pathname
* string is simply returned as if by the <code>{@link #getPath}</code>
* 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 <code>user.dir</code>, 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.
*
*<p>
<pre name="code" class="java"> <span style="font-family: 'Courier New';">* 返回抽象路径名的绝对路径名字符串。 </span>
<span style="font-family: 'Courier New';"> * 如果此抽象路径名已经是绝对路径名,则返回该路径名字符串,这与 getPath() 方法一样。</span>
* 如果此抽象路径名是空的抽象路径名,则返回当前用户目录的路径名字符串,该目录由系统属性 user.dir 指定。 * 否则,使用与系统有关的方式分析此路径名。在 UNIX 系统上,通过根据当前用户目录分析某一相对路径名, * 可使该路径名成为绝对路径名。在 Microsoft Windows 系统上, * 通过由路径名指定的当前驱动器目录(如果有)来分析某一相对路径名,可使该路径名成为绝对路径名; * 否则,可以根据当前用户目录来分析它。 * * @return 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 java.io.File#isAbsolute() */ public String getAbsolutePath() { return fs.resolve(this); }
最后看一下getCanonicalPath
/**
* Returns the canonical pathname string of this abstract pathname.
*
* <p> 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
* {@link #getAbsolutePath} method, and then maps it to its unique form in a
* system-dependent way. This typically involves removing redundant names
* such as <tt>"."</tt> and <tt>".."</tt> from the pathname, resolving
* symbolic links (on UNIX platforms), and converting drive letters to a
* standard case (on Microsoft Windows platforms).
*
* <p> 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.
*
* <p>
* 返回抽象路径名的规范路径名字符串。
* 规范路径名是绝对路径名,并且是惟一的。规范路径名的准确定义与系统有关。如有必要,
* 此方法首先将路径名转换成绝对路径名,这与调用 getAbsolutePath() 方法的效果一样,
* 然后用与系统相关的方式将它映射到其惟一路径名。
* 这通常涉及到从路径名中移除多余的名称(比如 "." 和 "..")、分析符号连接(对于 UNIX 平台),
* 以及将驱动器名转换成标准大小写形式(对于 Microsoft Windows 平台)。
* <p>
* 表示现有文件或目录的每个路径名都有一个惟一的规范形式。
* 表示非存在文件或目录的每个路径名也有一个惟一的规范形式。
* 非存在文件或目录路径名的规范形式可能不同于创建文件或目录之后同一路径名的规范形式。
* 同样,现有文件或目录路径名的规范形式可能不同于删除文件或目录之后同一路径名的规范形式。
*
*
* @return 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
*
* @throws SecurityException
* If a required system property value cannot be accessed, or
* if a security manager exists and its <code>{@link
* java.lang.SecurityManager#checkRead}</code> method denies
* read access to the file
*
* @since JDK1.1
* @see Path#toRealPath
*/
public String getCanonicalPath() throws IOException {
if (isInvalid()) {
throw new IOException("Invalid file path");
}
return fs.canonicalize(fs.resolve(this));
}