File 类的 getPath()、getAbsolutePath()、getCanonicalPath() 的区别【转】
File 类的 getPath()、getAbsolutePath()、getCanonicalPath() 的区别
感谢大佬:https://blog.csdn.net/zsensei/article/details/79365348
考虑一下几种路径:
C:\temp\file.txt- 绝对路径,也是规范路径.\file.txt- 相对路径C:\temp\myapp\bin\..\..\file.txt这是一个绝对路径,但不是规范路径
关于什么是规范路径可参考 What’s a “canonical path”?
粗略的认为规范路径就是不包含相对路径如 …\ 或者 .\ 的绝对路径
看一个例子:
import java.io.File;
public class PathTesting {
public static void main(String [] args) {
File f = new File("test/.././file.txt");
System.out.println(f.getPath());
System.out.println(f.getAbsolutePath());
try {
System.out.println(f.getCanonicalPath());
}
catch(Exception e) {}
}
}
上面的代码会输出
test…\file.txt
C:\projects\sandbox\trunk\test…\file.txt
C:\projects\sandbox\trunk\file.txt
由此可得出结论:
getPath()方法跟创建File对象时传入的路径参数有关,返回构造时传入的路径getAbsolutePath()方法返回文件的绝对路径,如果构造的时候是全路径就直接返回全路径,如果构造时是相对路径,就返回当前目录的路径 + 构造 File 对象时的路径getCanonicalPath()方法返回绝对路径,会把..\ 、.\这样的符号解析掉
参考:
What’s the difference between getPath(), getAbsolutePath(), and getCanonicalPath() in Java?
File 类的 getPath()、getAbsolutePath()、getCanonicalPath() 的区别【转】的更多相关文章
- java中File类的getPath(),getAbsolutePath(),getCanonicalPath()区别
File file = new File(".\\test.txt"); System.out.println(file.getPath()); System.out.printl ...
- file.getPath() getAbsolutePath() getCanonicalPath()区别
package file; import java.io.File; import java.io.IOException; public class getFilePath { public sta ...
- File类的createNewFile()与createTempFile()的区别
最近,在看代码时看到了一个方法, File.createTempFile() ,由此联想到File.createNewFile() 方法,一时间不知道两者到底有什么区别,感觉都是创建新文件嘛,后来查看 ...
- 【iOS】The differences between Class Extension and Header File 类扩展与头文件的区别
. As the name suggests, they extend the class. A class continuation is another name. The class exten ...
- Java 中File类的createNewFile()与createTempFile(), delete和deleteOnExit区别
1. Java 中File类的createNewFile()与createTempFile()的区别 最近,在看代码时看到了一个方法, File.createTempFile() ,由此联想到File ...
- 在外部存储器上写入或读取文件(Environment类、File类的使用)
1.Environment类 简单介绍:http://www.cnblogs.com/mengdd/p/3742623.html 详细介绍:http://www.2cto.com/kf/201408/ ...
- Java第二十九天,文件及目录的管理,File类
一.基础知识点 1.路径分隔符 (1)什么是路径分隔符? 这个多被应用在环境变量设置当中,例如当我设置Path环境变量时,多个环境变量的路径要用 ':'(Windows系统用封号分隔)或 ':'(Li ...
- Java【File类、递归】
File 1.在io包中 操作电脑中的文件和文件夹 java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和删除等操作. 我们可以使用File类的方法 创建一 ...
- 关于File.getPath,File.getAbsolutePath,File.getCanonicalPath的区别
这个问题, 不了解一下还是挺恍惚它们之间的区别的. 其实也挺简单的. getPath()-->>new File()时的路径 getAbsolutePath()-->>当前路径 ...
随机推荐
- CLION 使用自己的makefile来运行
之前参考过这里和这里,都是说要使用add_custom_target,无奈看不懂 但是前一篇里说它参考的是stackoverflow上的回答,去原帖里发现后来更新的第二高票答案!!!非常好用!!! 在 ...
- Reflection 基础知识(二)
Proxy 定义 Proxy用于修改对象的某些行为,获取值,设置值等 let p = new Proxy(target, handler); target 用Proxy包装的目标对象(可以是任何类型的 ...
- Spring企业级程序设计 • 【第3章 面向切面编程】
全部章节 >>>> 本章目录 3.1 AOP基本概念和术语 3.1.1 AOP概念 3.1.2 AOP的术语解释 3.1.3 通知类型介绍 3.1.4 通过AOP模拟事务操 ...
- 【MySQL作业】DDL 和 DML——美和易思使用 DML 删除表数据应用习题
点击打开所使用到的数据库>>> 删除客户"刘一鸣". 执行 SQL 代码"delete from customer where cName=' 刘一鸣 ...
- Drools集成SpringBootStarter
1.说明 基于fast-drools-spring-boot-starter, 能够方便的将规则引擎Drools集成到Spring Boot, 基于前面介绍过的文章Drools集成SpringBoot ...
- Python_对excel表格读写-openpyxl、xlrd&xlwt
openpyxl 和 xlrd&xlwt 都能对excel进行读写,但是它们读写的格式不同,openpyxl 只能读写 xlsx格式的excel,xlrd&xlwt 只能读写 xls格 ...
- 报错 java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp 原因
sql异常 java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestam ...
- Word2010制作饭店活动宣传单
原文链接: https://www.toutiao.com/i6492754127343321613/ 打开Word文档,选择"页面布局"选项卡."页面背景"功 ...
- 一文搞懂Flink Window机制
Windows是处理无线数据流的核心,它将流分割成有限大小的桶(buckets),并在其上执行各种计算. 窗口化的Flink程序的结构通常如下,有分组流(keyed streams)和无分组流(non ...
- Java NIO Channel 使用
Java NIO 中的 Channel 分类: FileChannel SocketChannel ServerSocketChannel DatagramChannel channel 分类 Fil ...