一、打开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. DataTable保存为Excel或者Txt

    保存为txt的时候,可保持原来的行列对齐,如下:using System; using System.Collections.Generic; using System.Text; using Sys ...

  2. FastDFS_v4.06安装简记

    提前准备所需4个包:FastDFS_v4.06.tar.gzfastdfs-nginx-module_v1.16.tar.gzlibevent-2.0.20-stable.tar.gznginx-1. ...

  3. 洛谷P3373 [模板]线段树 2(区间增减.乘 区间求和)

    To 洛谷.3373 [模板]线段树2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格 ...

  4. jsp下载word

    <%@ page language="java" contentType="application/msword;charset=utf-8"%> ...

  5. 2016年3月8日Android实习日记

    1.出现fragment后台栈的bug. bug描述:当点击加入后台栈的操作按钮改变指定控件的内容之后,称为A操作:接下来又点击其它没有操作后台栈的按钮来修改原来指定的控件内容,称为B操作.然后点击b ...

  6. webpack - HtmlWebpackPlugin理解

    该插件的两个主要作用: 为html文件中引入的外部资源如script.link动态添加每次compile后的hash,防止引用缓存的外部文件问题 可以生成创建html入口文件,比如单页面可以生成一个h ...

  7. How determine the RC time constant in PWM DAC low-pass filter?

    how determine the RC time constant in PWM digital to analog low-pass filter? I 'm looking for the be ...

  8. eclim CSearch macro 问题的解决过程备忘录

    问题: CSearch 宏时. RuntimeException: Could not find file with URI because it is a relative path, and no ...

  9. 连接mysql 出现:java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.

    数据测试的时候出现: 网上查资料说的是mysql5.x 版本和 8.x版本的区别: 5.7版本是:default_authentication_plugin=mysql_native_password ...

  10. 虚拟机下CentOS7开启SSH连接

    在虚拟机(Vmware Workstation)下,安装了CentOS7,现在想通过SSH工具连接虚拟机中的CentOS7 1.  首先,要确保CentOS7安装了  openssh-server,在 ...