一、打开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( ) 区别的更多相关文章

  1. java中File类的getPath(),getAbsolutePath(),getCanonicalPath()区别

    File file = new File(".\\test.txt"); System.out.println(file.getPath()); System.out.printl ...

  2. getCanonicalFile与getAbsoluteFile区别

    package test; import java.io.File; import java.io.IOException; public class TestFilePath { public st ...

  3. Java 中File类的createNewFile()与createTempFile(), delete和deleteOnExit区别

    1. Java 中File类的createNewFile()与createTempFile()的区别 最近,在看代码时看到了一个方法, File.createTempFile() ,由此联想到File ...

  4. File、Blob、ArrayBuffer等文件类的对象有什么区别和联系

    前言 在前端中处理文件时会经常遇到File.Blob.ArrayBuffer以及相关的处理方法或方式如FileReader.FormData等等这些名词,对于这些常见而又不常见的名词,我相信大多数人对 ...

  5. File 类的 getPath()、getAbsolutePath()、getCanonicalPath() 的区别【转】

    File 类的 getPath().getAbsolutePath().getCanonicalPath() 的区别 感谢大佬:https://blog.csdn.net/zsensei/articl ...

  6. File类与FileInfo类区别

    ile类是静态的,FileInfo不是静态的也没有静态的方法,仅可用于实例化的对象.FileInfo方法基本类似于File.关于二者,作何选择. ● 如果仅进行单一方法调用,则可以使用静态File类上 ...

  7. 第二十天File类、字节流

    File类.字节流 File类 File类介绍 File:它是描述持久设备上的文件或文件夹的.只要是在Java程序中操作文件或文件夹肯定需要使用File类完成. File类构造方法 /* * File ...

  8. Java之File类

    一.初见File类 java.io.File类代表系统中的文件(文件或目录) 常用构造方法 File(String pathname) File(String parent, String child ...

  9. Java中File类总结

    /** * @Title:JavaFile.java * @Package:com.yhd.chart.model * @Description:File类测试 * @author:Youhaidon ...

随机推荐

  1. 细说Vue作用域插槽,匹配应用场景。

    最近在官方文档中看到,vue新增了一种插槽机制,叫做作用域插槽.要求的版本是2.1.0+. 首先来说一下:顾名思义,所谓作用域插槽,主要就在作用域,需要注意的是(以下几点看不懂不要紧,配合下面的例子, ...

  2. Running multiple commands in one line in shell

      You are using | (pipe) to direct the output of a command into another command. What you are lookin ...

  3. Python3绘图库Matplotlib(02)

    控制颜色 Color Color Name b blue c cyan g green k black m magenta r red w white y yellow plt.plot(x1, y1 ...

  4. input输入框只能输入数字而且开头不能为零

    <div class="input-choseNum"> <input type="number" id="inp-chooseNu ...

  5. linux中内存超出后可以这样

    http://www.cnblogs.com/hongten/archive/2012/11/16/java_PermGen_space.html

  6. CentOS 6.8 安装 Erlang 及 RabbitMQ Server

    安装 Erlang 19.3 # 安装依赖包 yum install -y gcc gcc-c++ unixODBC-devel openssl-devel ncurses-devel # 下载 er ...

  7. ios webview下纯JS实现长按

    app,其中有长按LI列表弹出菜单,只要清楚五个方法就行:ontouchstart.ontouchmove.ontouchend.setTimeout.clearTimeout 1.首先在我们按下手指 ...

  8. CustomJsonDateDeserializer @JsonDeserialize(using = CustomJsonDateDeserializer.class) Jackson 反序列化Date时遇到的问题 java中json日期属性反序列化

    public class CustomJsonDateDeserializer extends JsonDeserializer<Date> { @Override public Date ...

  9. springboot自定义http反馈状态码

    最近在开发一些http server类型程序,通过spring boot构建一些web程序,这些web程序之间通过http进行数据访问.共享,如下图: 假设现在client发起一次保存数据的请求到se ...

  10. Android学习笔记(11):线性布局LinearLayout

    线性布局LinearLayout是指在横向或是竖向一个接一个地排列.当排列的组件超出屏幕后,超出的组件将不会再显示出来. LinearLayout支持的XML属性和相应方法如表所看到的: Attrib ...