File 类的 getCanonicalFile( ) 和 getAbsoluteFile( ) 区别

一、打开java.io.File源码,看下两个方法的区别

getAbsoluteFile

 

public File getAbsoluteFile() { 
String absPath = getAbsolutePath();
return new File(absPath, fs.prefixLength(absPath));
}

 

getCanonicalFile

 

public File getCanonicalFile() throws IOException { 
String canonPath = getCanonicalPath();
return new File(canonPath, fs.prefixLength(canonPath));
}

 

通过源码我们可以到 getAbsoluteFile 是不会抛出异常的,而 getCanonicalFile 会抛出 IOException,两个方法的区别主要体现在所对应的 getAbsolutePath 方法和 getCanonicalPath 上。

 

二、方法 getAbsolutePath 与 getCanonicalPath 分析

1. 比对两个方法源码区别

 

public String getAbsolutePath() {
return fs.resolve(this);
}

 

public String getCanonicalPath() throws IOException {
if (isInvalid()) {
throw new IOException("Invalid file path");
}
return fs.canonicalize(fs.resolve(this));
}

 

 

从代码上看 getCanonicalPath 比 getAbsolutePath 多了 fs.canonicalize 操作,而这个 FileSystem 类是与操作系统相关的。

2. 从 javadoc 说明上看

 

public String getAbsolutePath() 

返回此抽象路径名的绝对路径名字符串。

如果此抽象路径名已经是绝对路径名,则返回该路径名字符串,这与 getPath( ) 方法一样。如果此抽象路径名是空抽象路径名,则返回当前用户目录的路径名字符串,该目录由系统属性 user.dir 指定。否则,使用与系统有关的方式解析此路径名。在 UNIX 系统上,根据当前用户目录解析相对路径名,可使该路径名成为绝对路径名。在 Microsoft Windows 系统上,根据路径名指定的当前驱动器目录(如果有)解析相对路径名,可使该路径名成为绝对路径名;否则,可以根据当前用户目录解析它。

 

public String getCanonicalPath() throws IOException 

返回此抽象路径名的规范路径名字符串。

规范路径名是绝对路径名,并且是惟一的。规范路径名的准确定义与系统有关。如有必要,此方法首先将路径名转换为绝对路径名,这与调用 getAbsolutePath( ) 方法的效果一样,然后用与系统相关的方式将它映射到其惟一路径名。这通常涉及到从路径名中移除多余的名称(比如 "." 和 "..")、解析符号连接(对于 UNIX 平台),以及将驱动器号转换为标准大小写形式(对于 Microsoft Windows 平台)。

每个表示现存文件或目录的路径名都有一个惟一的规范形式。每个表示不存在文件或目录的路径名也有一个惟一的规范形式。不存在文件或目录路径名的规范形式可能不同于创建文件或目录之后同一路径名的规范形式。同样,现存文件或目录路径名的规范形式可能不同于删除文件或目录之后同一路径名的规范形式。

 

由此我们可知:getCanonicalPath 会将文件路径解析为与操作系统相关的唯一的规范形式的字符串,而 getAbsolutePath 并不会。

 

三、测试代码

 

@Test
public void test01() throws Exception {
File file2 = new File("D:\\JetBrains\\LitterRoach\\javaDevelopworks\\target\\classes\\java.policy");
System.out.println("-----默认绝对路径:取得路径相同------");
System.out.println("getPath: " + file2.getAbsolutePath ());
System.out.println("getCanonicalPath: " + file2.getCanonicalPath());
}

/**
* -----默认绝对路径:取得路径相同------
* getPath: D:\JetBrains\LitterRoach\javaDevelopworks\target\classes\java.policy
* getCanonicalPath: D:\JetBrains\LitterRoach\javaDevelopworks\target\classes\java.policy
*
*/

@Test
public void test02() throws IOException {
//文件存在
System.out.println("=======文件存在=========");
File file1 = new File("./target/classes/JAVA.POLICY");
System.out.println("getPath: " + file1.getAbsolutePath ());
System.out.println("getCanonicalPath: " + file1.getCanonicalPath());

//文件不存在
System.out.println("=======文件不存在=========");
File file2 = new File("JAVA.POLICY");
System.out.println("getPath: " + file2.getAbsolutePath ());
System.out.println("getCanonicalPath: " + file2.getCanonicalPath());
}

/**
* =======文件存在=========
* getPath: D:\JetBrains\LitterRoach\javaDevelopworks\.\target\classes\JAVA.POLICY
* getCanonicalPath: D:\JetBrains\LitterRoach\javaDevelopworks\target\classes\java.policy
* =======文件不存在=========
* getPath: D:\JetBrains\LitterRoach\javaDevelopworks\JAVA.POLICY
* getCanonicalPath: D:\JetBrains\LitterRoach\javaDevelopworks\JAVA.POLICY
*/

 

转载于:https://www.cnblogs.com/tuhooo/p/10599485.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值