一、打开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. 大数据系列博客之 --- 深入简出 Shell 脚本语言(高级篇)

    首先声明,此系列shell系列博客分为四篇发布,分别是: 基础篇:https://www.cnblogs.com/lsy131479/p/9914747.html 提升篇:https://www.cn ...

  2. [Agc005D]K Perm Counting

    [Agc005D] K Perm Counting Description 糟糕爷特别喜爱排列.他正在构造一个长度为N的排列.但是他特别讨厌正整数K.因此他认为一个排列很糟糕,当且仅当存在至少一个i( ...

  3. java后端发送请求

    package com.ty.mapapisystem.util; import java.io.BufferedReader;import java.io.FileInputStream;impor ...

  4. 在web.xml中配置Spring的application.xml

    <context-param> <!-- 请记住这里的名称不能够乱写,必须一模一样,区分大小写 java类去启动项目 contextClass--> <param-nam ...

  5. [CF920G]List Of Integers

    Description: \(t\)组询问,求第\(k\)个大于\(x\)且与\(p\)互质的数 Hint: \(x,k,p<=1e6,t<=30000\) Solution: 推出式子后 ...

  6. Docker machine(Docker 虚拟机)

    安装docker [root@lianxi ~]# yum -y install docker 启动docker [root@lianxi ~]# systemctl start docker 下载D ...

  7. Codeforces Round #410 (Div. 2) 题解 【ABCD】

    A 题意:问你恰好修改一个字符,能不能使得字符串变成回文串 题解:显然直接for一遍,如果长度为偶数,那么不一样的必须是1个:如果长度为奇数,那么不一样的也可以是0个 #include<bits ...

  8. Linux shell命令 cp 加上-f还是提示是否覆盖

    这是由于环境变量中有 allias cp='cp -i' 为了去掉这个系统自带的别名,能够使用grep -r --include="*" "alias cp"  ...

  9. centos npm 安装后 command not found

    ok,

  10. 使用Doxygen + graphviz生成Unity 3d的UGUI类图

    下载软件 1) Graphviz,下载地址:http://download.csdn.net/detail/u010953266/8591169 为什么不用官网?一是下载速度慢,二是下载到本地的文件貌 ...