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

通过阅读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()返回绝对路径,可以解析出/./

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值