首页 > 其他 > 详细

File:getPath()&getAbsolutePath()&getCanonicalPath()

时间:2015-08-21 00:16:36      阅读:460      评论:0      收藏:0      [点我收藏+]

通过阅读API文档,下面是3个方法的讲解截图:
技术分享

技术分享

技术分享

getPath()&getAbsolutePath()的区别
getPath()得到的是构造file的时候的路径
getAbsolutePath()得到的是全路径

 File file=new File("e:/111");
 System.out.println(file.getPath());  //e:\111
 System.out.println(file.getAbsolutePath());  //e:\111
     try {      System.out.println(file.getCanonicalPath());//E:\111
    } catch (IOException e1) {
    e1.printStackTrace();
            }

上面3个函数得到的结果是一样的
那么在看下面的代码

  File file=new File("./111/test");
      System.out.println(file.getPath());
      System.out.println(file.getAbsolutePath());  
             try {
System.out.println(file.getCanonicalPath());
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

运行结果:

.\111\test
D:\work\tests\.\111\test
D:work\tests\111\test

work是workspace,tests是工程。
通过上面的结果getPath()返回是File构造是的路径,如果够着的绝对路径那么就返回绝对路径,如果构造不是绝对路径,则相应返回的就是构造时的路径。
getAbsolutePath()返回的是绝对路径,但是其得到的结果为:D:\work\tests.\111\test。无法解析出/./
getCanonicalPath()返回绝对路径,可以解析出/./

版权声明:本文为博主原创文章,未经博主允许不得转载。

File:getPath()&getAbsolutePath()&getCanonicalPath()

原文:http://blog.csdn.net/qq_17326933/article/details/47817327

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!