getPath vs getAbsolutePath vs getCanonicalPath
大约 2 分钟
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
系统推荐
- MAT工具
- JVM垃圾收集器
- Git合并多个提交并push到远程仓库
- JVM参数设置
- Nacos-Spring Gateway-Spring boot无感发布
- 提取Docker镜像中的文件
- 线上FullGC频繁的排查
- 批量修改git历史记录中的用户名和邮箱
- 常用队列
- 简易版配置中心&初探原理
- 不重启 JVM,如何替换掉已经加载的类?
- ES杂项
- 随机毒鸡汤:你喜欢什么样的男人?我喜欢笑起来会发光的男人。你是说如来吗?