getPath
:返回我们构造File的时候填充的路径参数:eg:..\test.txt
public String getPath() {return path;}
getAbsolutePath
:返回绝对路径,路径中如果包含.
或者..
不会进行路径转换,eg:G:\qsbk\..\test.txt
public String getAbsolutePath() {return fs.resolve(this);}
getCanonicalPath
:返回规范化路径名的绝对路径,即:会将路径中的.
或者..
进行最终转换,例如上述的路径结果是:eg:G:\test.txt
public String getCanonicalPath() throws IOException {
if (isInvalid()) {
throw new IOException("Invalid file path");
}
return fs.canonicalize(fs.resolve(this));
}
下面我们看一个实例:
public static void main(String[] args) throws IOException {
File file = new File("../test.txt");
String str1 = file.getPath();
System.out.println(str1);
str1 = file.getAbsolutePath();
System.out.println(str1);
str1 = file.getCanonicalPath();
System.out.println(str1);
}
输出: