【查漏补缺】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初始化十几篇文章). 检查激 ...
随机推荐
- 36ArcGIS API for JavaScript3.X 系列加载天地图(经纬度)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Python3小知识点
1. 是转义字符,\可以输出.在一行的末尾单独的一个\表示这行没有结束,下一行接着写 2.可以用"'或"""把一大段话引起来(可以换行)然后赋值,输出. 3.要 ...
- PTA第四次作业
题目 7-1 计算职工工资 1.设计思路 (1)第一步:观察题意了解各个参数与所需函数在题目中的意义: 第二步:设计算法编写函数,让函数的功能实现题目中所需的功能: 第三步:运行程序检测是否错误. ( ...
- Openssl的证书操作
先假设自己是一个CA,而且是一个root CA,Cliu8CA 生成一个CA的private key openssl genrsa -out caprivate.key 1024 当然可以跟密码 op ...
- JS闭包解析
三点注意事项 JS作用域传送门 JS没有块级作用域,只有全局作用域和局部作用域(函数作用域). JS中的作用域链,内部的作用域可以访问到外部作用域中的变量和方法,而外部作用域不能访问内部作用域的变量和 ...
- [Swift]LeetCode56. 合并区间 | Merge Intervals
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- [SQL]LeetCode262.行程和用户 | Trips and Users
SQL架构 Create table If Not Exists Trips (Id )) Create table If Not Exists Users (Users_Id ), Role ENU ...
- [Swift]LeetCode514. 自由之路 | Freedom Trail
In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal ...
- [Swift]LeetCode825. 适龄的朋友 | Friends Of Appropriate Ages
Some people will make friend requests. The list of their ages is given and ages[i] is the age of the ...
- 通信统计接口字段填充内容shell脚本
现在通信行业上班,最麻烦的就是计算各个协议接口字段填充的内容是否正确.经历了小白似的一个个字段统计在到单个接口所有字段统计,然后到现在的多个接口一次统计.在大大节省了时间的同时,让自己有更充足的时间来 ...