getPath vs getAbsolutePath vs getCanonicalPath
getPath vs getAbsolutePath vs getCanonicalPath
-
getPath
以构造路径作为返回值
-
getAbsolutePath
以绝对路径作为返回值
-
getCanonicalPath
以绝对路径作为返回值(如果路径包含.或..会进行处理)
public static void main(String[] args){
File file1 = new File(".\\xxx.txt");
File file2 = new File("D:\\aaa\\xxx.txt");
System.out.println(file1.getPath());
System.out.println(file1.getAbsolutePath());
System.out.println(file1.getCanonicalPath());
System.out.println("------------------------");
System.out.println(file2.getPath());
System.out.println(file2.getAbsolutePath());
System.out.println(file2.getCanonicalPath());
}
输出如下内容:
.\xxx.txt
D:\aaa\.\xxx.txt
D:\aaa\xxx.txt
------------------------
D:\aaa\xxx.txt
D:\aaa\xxx.txt
D:\aaa\xxx.txt
系统推荐
- Cordova+Umi项目搭建步骤
- Git笔记
- 你真的会拼接字符串吗?
- Thread & ExecutorService & ThreadPoolExecutor 总览
- InnoDB存储引擎
- Arthas使用记录
- 简易版配置中心&初探原理
- SQL优化
- Lombok的Accessors导致EasyExcel读取失败
- 批量替换文件名中的指定字符串
- Mermaid示例
- CentOS7下Docker端口映射后防火墙失效
- 随机毒鸡汤:你看人家鸭子,水上面保持着冷静,水下面拼命划水。
文章来源于justsoso.fun: getPath vs getAbsolutePath vs getCanonicalPath