详谈java中File类getPath()、getAbsolutePath()、getCanonical的区别
作者:jingxian 时间:2022-08-18 19:21:44
简单看一下描述,例子最重要。
1、getPath():
返回定义时的路径,(就是你写什么路径,他就返回什么路径)
2、getAbsolutePath():
返回绝对路径,但不会处理“.”和“..”的情况
3、getCanonicalPath():
返回的是规范化的绝对路径,相当于将getAbsolutePath()中的“.”和“..”解析成对应的正确的路径
第一个例子:(使用:“.\\src\\test.txt”一个点路径)
File file = new File(".\\src\\test.txt");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
System.out.println( file.getCanonicalPath());
结果:
.\src\test.txt
D:\workspace\java_io\.\src\test.txt
D:\workspace\java_io\src\test.txt
第二个例子:(使用:“..\\src\\test.txt”两个点路径)
File file = new File("..\\src\\test.txt");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
System.out.println( file.getCanonicalPath());
结果:
..\src\test.txt
D:\workspace\java_io\..\src\test.txt
D:\workspace\src\test.txt 注意这个结果的路径,与一个点时不一样;因为他解析了“.”和“..”的情况。
第三个例子:(使用:“D:\\test\\absoluteTest.txt”绝对路径)
File file = new File("D:\\test\\absoluteTest.txt");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
System.out.println(file.getCanonicalPath());
结果:
D:\test\absoluteTest.txt
D:\test\absoluteTest.txt
D:\test\absoluteTest.txt
结论:
1、当输入为绝对路径时,返回的都是绝对路径。
2、当输入为相对路径时:
2.1、getPath()返回的是File构造方法里的路径,是什么就是什么,不增不减
2.2、getAbsolutePath()返回的其实是user.dir+getPath()的内容,从上面看:D:\workspace\java_io\.\src\test.txt,D:\workspace\java_io\..\src\test.txt,可以得出。
2.3、getCanonicalPath()返回的就是标准的将符号完全解析的路径
标签:java,File类,getPath,getAbsolutePath,getCanonical
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot Thymeleaf实现国际化的方法详解
2023-11-24 06:01:01
![](https://img.aspxhome.com/file/2023/8/59008_0s.png)
Spring Boot 实现配置文件加解密原理
2023-11-23 17:48:46
![](https://img.aspxhome.com/file/2023/4/59584_0s.jpg)
Java 字符串转float运算 float转字符串的方法
2022-04-09 10:09:06
java判断http地址是否连通(示例代码)
2023-08-05 03:24:05
关于Java中增强for循环使用的注意事项
2021-08-09 16:47:43
![](https://img.aspxhome.com/file/2023/7/63307_0s.png)
Java使用HttpUtils实现发送HTTP请求
2021-06-11 07:08:39
![](https://img.aspxhome.com/file/2023/4/61034_0s.jpg)
IDEA中配置文件模板的添加方法
2023-10-28 17:52:31
![](https://img.aspxhome.com/file/2023/4/58674_0s.jpg)
Java经典面试题最全汇总208道(四)
2023-11-08 23:59:26
![](https://img.aspxhome.com/file/2023/5/59065_0s.png)
使用Spring Cloud Feign远程调用的方法示例
2021-12-06 10:30:09
同时使用@LoadBalanced @RefreshScope注解负载均衡失效分析
2023-12-07 10:59:24
![](https://img.aspxhome.com/file/2023/7/62037_0s.png)
Java中ArrayList初始化的四种方法详解
2022-03-29 21:50:13
详解Java中final的用法
2022-09-22 10:20:32
Android 中TextView中跑马灯效果的实现方法
2023-08-07 01:39:46
Spring Boot 定义系统启动任务的多种方式
2023-11-24 13:25:33
![](https://img.aspxhome.com/file/2023/7/61947_0s.png)
Spring Cache+Redis缓存数据的实现示例
2023-11-26 11:53:20
![](https://img.aspxhome.com/file/2023/0/60060_0s.png)
10种简单的Java性能优化
2023-06-20 20:43:41
![](https://img.aspxhome.com/file/2023/6/57526_0s.png)
spring boot集成smart-doc自动生成接口文档详解
2023-11-28 23:08:02
![](https://img.aspxhome.com/file/2023/3/60773_0s.png)
Spring Cloud 的 Hystrix.功能及实践详解
2023-11-19 06:40:46
![](https://img.aspxhome.com/file/2023/9/59249_0s.jpg)
Java流程控制语句之If选择结构
2023-11-11 04:02:29
![](https://img.aspxhome.com/file/2023/3/58853_0s.png)
Java客户端调用.NET的WebService实例
2023-11-03 17:22:00
![](https://img.aspxhome.com/file/2023/8/58988_0s.png)