getPath() getAbsolutePath() getCanonicalPath()不同之处
2016-04-26 00:00
561 查看
摘要: What's the difference between getPath(), getAbsolutePath(), and getCanonicalPath() in Java?
可以用上面代码测试一下。
Your output will be something like:
总结来看:getPath() 是 获得这个File对象所传入的path 参数。
总结来看:getAbsolutePath() 是获得绝对路径,但是不对getPath里面的相对路径进行转换。
总结来看:getCanonicalPath() 是对getAbsolutePath()中间的相对路径进行转换。
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) {} } }
可以用上面代码测试一下。
Your output will be something like:
test\..\.\file.txt C:\projects\sandbox\trunk\test\..\.\file.txt C:\projects\sandbox\trunk\file.txt
getPath()gets the path string that the
Fileobject was constructed with, and it may be relative current directory.
总结来看:getPath() 是 获得这个File对象所传入的path 参数。
getAbsolutePath()gets the path string after resolving it against the current directory if it's relative, resulting in a fully qualified path.
总结来看:getAbsolutePath() 是获得绝对路径,但是不对getPath里面的相对路径进行转换。
getCanonicalPath()gets the path string after resolving any relative path against current directory, and removes any relative pathing (
.and
..), and any file system links to return a path which the file system considers the canonical means to reference the file system object to which it points.
总结来看:getCanonicalPath() 是对getAbsolutePath()中间的相对路径进行转换。
相关文章推荐
- ThinkPHP关联查询
- [php-src]窥探Php内核中的变量
- PHP5 String 函数(更新中)
- EasyPusher RTSP直播之RTP数据包格式解析
- EasyPusher RTSP直播之RTP数据包格式解析
- phpstorm 控制台运行
- yii2基础(二)
- phpStudy升级MySQL教程
- laravel 入门基础之安装
- PHP中的$this用法
- PHP读取超大文件的最后几行
- yii2基础(一)
- Yii
- 涨薪的php学习路线图2016版
- php发送email最终版
- mail_send.php
- mail.class.php
- class.phpmailer.php(email这个类)
- 4月24日 PHP函数
- php基础之三 数组