【查漏补缺】File的path、absolutePath和canonicalPath的区别
背景
在学习Idea的插件开发时,用到了相关的VirtualFileSystem
这个东西,里面的VirtualFile
有一个getCanonicalPath()
方法引起了我的注意,我发现我不知道——
科普
首先知晓一下几个名词——路径、绝对路径/相对路径、规范路径
然后考虑以下几种路径:
- c:\temp\file.txt
- .\file.txt
- c:\temp\MyApp\bin\..\..\file.txt
第一类,属于路径,绝对路径,规范路径
第二类,属于路径,相对路径
第三类,属于路径,绝对路径
我们结合自己的开发经验,发现绝大多数情况都已经被覆盖到了,那么我们可以大致推测,路径
包含绝对路径/相对路径
,绝对路径
包含规范路径
,而相对路径
不包含规范路径
。
实战
/* -------这是一个规范路径的代码------- */
File file = new File("C:\\Users\\W650\\Desktop\\701Studio\\app.js");
System.out.println("file.getAbsolutePath() -> " + file.getAbsolutePath());
System.out.println("file.getCanonicalPath() -> " + file.getCanonicalPath());
System.out.println("file.getPath() -> " + file.getPath());
/* -------输出------- */
file.getAbsolutePath() -> C:\Users\W650\Desktop\701Studio\app.js
file.getCanonicalPath() -> C:\Users\W650\Desktop\701Studio\app.js
file.getPath() -> C:\Users\W650\Desktop\701Studio\app.js
/* -------这是一个绝对路径的代码(但不规范)------- */
File file = new File("C:\\Users\\W650\\Desktop\\701Studio\\utils\\..\\app.js");
System.out.println("file.getAbsolutePath() -> " + file.getAbsolutePath());
System.out.println("file.getCanonicalPath() -> " + file.getCanonicalPath());
System.out.println("file.getPath() -> " + file.getPath());
/* -------输出------- */
file.getAbsolutePath() -> C:\Users\W650\Desktop\701Studio\utils\..\app.js
file.getCanonicalPath() -> C:\Users\W650\Desktop\701Studio\app.js
file.getPath() -> C:\Users\W650\Desktop\701Studio\utils\..\app.js
/* -------这是一个相对路径的代码------- */
File file = new File("..\\..\\..\\Test.txt");
System.out.println("file.getAbsolutePath() -> " + file.getAbsolutePath());
System.out.println("file.getCanonicalPath() -> " + file.getCanonicalPath());
System.out.println("file.getPath() -> " + file.getPath());
/* -------输出------- */
file.getAbsolutePath() -> E:\commonWorkspace\IdeaPluginDevGuide\DevGuide-VirtualFileSystem\..\..\..\Test.txt
file.getCanonicalPath() -> E:\Test.txt
file.getPath() -> ..\..\..\Test.txt
区别与联系
Returns the canonical pathname string of this abstract pathname.
<p> A canonical pathname is both absolute and unique. The precise
definition of canonical form is system-dependent. This method first
converts this pathname to absolute form if necessary, as if by invoking the
{@link #getAbsolutePath} method, and then maps it to its unique form in a
system-dependent way. This typically involves removing redundant names
such as <tt>"."</tt> and <tt>".."</tt> from the pathname, resolving
symbolic links (on UNIX platforms), and converting drive letters to a
standard case (on Microsoft Windows platforms).
<p> Every pathname that denotes an existing file or directory has a
unique canonical form. Every pathname that denotes a nonexistent file
or directory also has a unique canonical form. The canonical form of
the pathname of a nonexistent file or directory may be different from
the canonical form of the same pathname after the file or directory is
created. Similarly, the canonical form of the pathname of an existing
file or directory may be different from the canonical form of the same
pathname after the file or directory is deleted.
大概就是说getCanonicalPath()
获得的格式是和系统相关的(Linux和Windows下不一样),在执行过程中会将当前的path
转换成absolute path
,然后去掉absolute path
内重复的 .和 ..等等。
最后一段有点不理解,就是说
* 表示现有文件或目录的每个路径名都有一个惟一的规范形式。
* 表示非存在文件或目录的每个路径名也有一个惟一的规范形式。
* 非存在文件或目录路径名的规范形式可能不同于创建文件或目录之后同一路径名的规范形式。
* 同样,现有文件或目录路径名的规范形式可能不同于删除文件或目录之后同一路径名的规范形式。
结论
- 路径包含绝对路径和相对路径,绝对路径又包含了规范路径。
getPath()
会返回给用户创建File的路径,getAbsolutePath
会依据调用该方法的类所在的路径 + 文件分隔符 + 创建File的路径
构造绝对路径,getCanonicalPath()
一定返回规范的路径。
参考
What's the difference between getPath(), getAbsolutePath(), and getCanonicalPath() in Java?
【查漏补缺】File的path、absolutePath和canonicalPath的区别的更多相关文章
- 20165223 week1测试查漏补缺
week1查漏补缺 经过第一周的学习后,在蓝墨云班课上做了一套31道题的小测试,下面是对测试题中遇到的错误的分析和总结: 一.背记题 不属于Java后继技术的是? Ptyhon Java后继技术有? ...
- Java基础查漏补缺(2)
Java基础查漏补缺(2) apache和spring都提供了BeanUtils的深度拷贝工具包 +=具有隐形的强制转换 object类的equals()方法容易抛出空指针异常 String a=nu ...
- Java基础查漏补缺(1)
Java基础查漏补缺 String str2 = "hello"; String str3 = "hello"; System.out.println(str3 ...
- 《CSS权威指南》基础复习+查漏补缺
前几天被朋友问到几个CSS问题,讲道理么,接触CSS是从大一开始的,也算有3年半了,总是觉得自己对css算是熟悉的了.然而还是被几个问题弄的"一脸懵逼"... 然后又是刚入职新公司 ...
- js基础查漏补缺(更新)
js基础查漏补缺: 1. NaN != NaN: 复制数组可以用slice: 数组的sort.reverse等方法都会改变自身: Map是一组键值对的结构,Set是key的集合: Array.Map. ...
- Entity Framework 查漏补缺 (一)
明确EF建立的数据库和对象之间的关系 EF也是一种ORM技术框架, 将对象模型和关系型数据库的数据结构对应起来,开发人员不在利用sql去操作数据相关结构和数据.以下是EF建立的数据库和对象之间关系 关 ...
- 2019Java查漏补缺(一)
看到一个总结的知识: 感觉很全面的知识梳理,自己在github上总结了计算机网络笔记就很累了,猜想思维导图的方式一定花费了作者很大的精力,特共享出来.原文:java基础思维导图 自己学习的查漏补缺如下 ...
- 今天開始慢下脚步,開始ios技术知识的查漏补缺。
从2014.6.30 開始工作算起. 如今已经是第416天了.不止不觉.时间过的真快. 通过对之前工作的总结.发现,你的知识面.会决定你面对问题时的态度.过程和结果. 简单来讲.知识面拓展了,你才干有 ...
- Mysql查漏补缺笔记
目录 查漏补缺笔记2019/05/19 文件格式后缀 丢失修改,脏读,不可重复读 超键,候选键,主键 构S(Stmcture)/完整性I(Integrity)/数据操纵M(Malippulation) ...
- 【spring源码分析】IOC容器初始化——查漏补缺(四)
前言:在前几篇查漏补缺中,其实我们已经涉及到bean生命周期了,本篇内容进行详细分析. 首先看bean实例化过程: 分析: bean实例化开始后 注入对象属性后(前面IOC初始化十几篇文章). 检查激 ...
随机推荐
- scala Actor Akka
推荐博客:过往记忆 https://www.iteblog.com/archives/1154.html akka.io
- Hadoop 操作常见问题解决
1. 安全模式下不可操作 提示信息: Hadoop "Cannot create directory .Name node is in safe mode." 解决方法: $ ha ...
- 一个开源的,跨平台的.NET机器学习框架ML.NET
微软在Build 2018大会上推出的一款面向.NET开发人员的开源,跨平台机器学习框架ML.NET. ML.NET将允许.NET开发人员开发他们自己的模型,并将自定义ML集成到他们的应用程序中,而无 ...
- 字符串匹配(一)----Rabin-Karp算法
题目:假如要判断字符串A"ABA"是不是字符串B"ABABABA"的子串. 解法一:暴力破解法, 直接枚举所有的长度为3的子串,然后依次与A比较,这样就能得出匹 ...
- Node.js(day6)
初始化准备工作 初始化目录 nmp init -y 安装基本的第三方插件 express npm install express --save art-template npm install art ...
- [Swift]LeetCode547. 朋友圈 | Friend Circles
There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...
- Ceres Solver 在win8+vs2013环境下的安装
参考博文:https://blog.csdn.net/wzheng92/article/details/79504709
- MySQL优化之my.conf配置详解
最近项目不太忙,所以有时间静心来研究下mysql的优化,对于MySQL的设置是否合理优化,直接影响到网站的速度和承载量!同时,MySQL也是优化难度最大的一个部分,不但需要理解一些MySQL专业知识, ...
- [Abp 源码分析]一、Abp 框架启动流程分析
Abp 不一定仅用于 Asp.Net Core 项目,他也可以在 Console 与 WinFrom 项目当中进行使用,所以关于启动流程可以分为两种,一种是 Asp.Net Core 项目的启动流程, ...
- 【Impala篇】---Hue从初始到安装应用
一.前述 Cloudera公司推出,提供对HDFS.Hbase数据的高性能.低延迟的交互式SQL查询功能.基于Hive使用内存计算,兼顾数据仓库.具有实时.批处理.多并发等优点 是CDH平台首选的PB ...