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 并不会。
三、测试代码
@Testpublic 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 * */
@Testpublic 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 */
File 类的 getCanonicalFile( ) 和 getAbsoluteFile( ) 区别的更多相关文章
- java中File类的getPath(),getAbsolutePath(),getCanonicalPath()区别
File file = new File(".\\test.txt"); System.out.println(file.getPath()); System.out.printl ...
- getCanonicalFile与getAbsoluteFile区别
package test; import java.io.File; import java.io.IOException; public class TestFilePath { public st ...
- Java 中File类的createNewFile()与createTempFile(), delete和deleteOnExit区别
1. Java 中File类的createNewFile()与createTempFile()的区别 最近,在看代码时看到了一个方法, File.createTempFile() ,由此联想到File ...
- File、Blob、ArrayBuffer等文件类的对象有什么区别和联系
前言 在前端中处理文件时会经常遇到File.Blob.ArrayBuffer以及相关的处理方法或方式如FileReader.FormData等等这些名词,对于这些常见而又不常见的名词,我相信大多数人对 ...
- File 类的 getPath()、getAbsolutePath()、getCanonicalPath() 的区别【转】
File 类的 getPath().getAbsolutePath().getCanonicalPath() 的区别 感谢大佬:https://blog.csdn.net/zsensei/articl ...
- File类与FileInfo类区别
ile类是静态的,FileInfo不是静态的也没有静态的方法,仅可用于实例化的对象.FileInfo方法基本类似于File.关于二者,作何选择. ● 如果仅进行单一方法调用,则可以使用静态File类上 ...
- 第二十天File类、字节流
File类.字节流 File类 File类介绍 File:它是描述持久设备上的文件或文件夹的.只要是在Java程序中操作文件或文件夹肯定需要使用File类完成. File类构造方法 /* * File ...
- Java之File类
一.初见File类 java.io.File类代表系统中的文件(文件或目录) 常用构造方法 File(String pathname) File(String parent, String child ...
- Java中File类总结
/** * @Title:JavaFile.java * @Package:com.yhd.chart.model * @Description:File类测试 * @author:Youhaidon ...
随机推荐
- linux manjaro 配置 pytorch gpu 环境
manjaro目前中国资料偏少,踩了很多坑. 安装gpu版本就这么几个步骤 1 安装英伟达的驱动cuda 2 安装 cudnn 3 安装支持gpu的pytorch 或者其他的运算框架 manja ...
- BZOJ.4241.历史研究(回滚莫队 分块)
题目链接 \(Description\) 长度为n的数列,m次询问,每次询问一段区间最大的 \(A_i*tm_i\) (重要度*出现次数) \(Solution\) 好像可以用莫队做,但是取max的操 ...
- Python3字符串-最容易理解的方式
字符串的创建 字符串创建符号 ' ' " " ''' ''' """ """ 转义符\ >>> str ...
- [HackerRank]Choosing White Balls
[HackerRank]Choosing White Balls 题目大意: 有\(n(n\le30)\)个球排成一行,每个球的颜色为黑或白. 执行\(k\)次操作,第\(i\)次操作形式如下: 从\ ...
- APP支付宝支付接入
1.app支付简介 买家可以在手机,掌上电脑等无线设备的应用程序内,通过支付宝(支付宝app或网页版支付宝)付款购买商品,且资金实行实时到账. 2.申请条件 1.申请前必须拥有经过实名认证的支付宝账户 ...
- [TenserFlow学习笔记]——安装
最近人工智能.深度学习.机器学习等词汇很是热闹,所以想进一步学习一下.不一定吃这口饭,但多了解一下没有坏处.接下来将学习到的一些知识点做一下记录. 1.安装环境 在VMWare虚拟机中安装最新版本的U ...
- boost 1.67编译VS2017版本
最近想系统学习并使用一下boost的asio异步网络库,所以需要编译boost库使用,下面简单介绍如何编译. 编译环境 boost1.67版本,windows 10,VS2017 下载boost 建议 ...
- RxJava2学习笔记(2)
上一篇已经熟悉了Observable的基本用法,但是如果仅仅只是“生产-消费”的模型,这就体现不出优势了,java有100种办法可以玩这个:) 一.更简单的多线程 正常情况下,生产者与消费者都在同一个 ...
- mysql5.7报err 1055错误 sql_mode=only_full_group_by
vim /etc/my.cnf 末尾增加 sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_B ...
- Windows IIS注册asp 此操作系统版本不支持此选项 错误解决方法
更新Win10,原来的IIS站点访问不了,原因是因为IIS 没有.net 4.5,使用网上的aspnet_regiis.exe -i命令,一点都不靠谱,直接提示: C:\WINDOWS\system3 ...