java路径相关(相对路径,绝对路径,抽象路径)

public static final String WEB_ROOT =  System.getProperty("user.dir") + File.separator  + "webroot";

File file = new File(WEB_ROOT+"\\a.html");

System.out.println("相对路径:"+file.getPath());
System.out.println("绝对路径:"+file.getAbsolutePath());
System.out.println("抽象路径:"+file.getCanonicalPath());

相对路径:E:\mystudy\myTomcat\webroot\a.html
绝对路径:E:\mystudy\myTomcat\webroot\a.html
抽象路径:E:\mystudy\myTomcat\webroot\a.html

File file = new File("./\\webroot\\b2.txt");

System.out.println("相对路径:"+file.getPath());
System.out.println("绝对路径:"+file.getAbsolutePath());
System.out.println("抽象路径:"+file.getCanonicalPath());

相对路径:.\webroot\b2.txt
绝对路径:E:\mystudy\myTomcat\.\webroot\b2.txt
抽象路径:E:\mystudy\myTomcat\webroot\b2.txt
当输入为绝对路径时,返回的都是绝对路径。
当输入为相对路径时:
getPath()返回的是File构造方法里的路径
getAbsolutePath()返回的其实是user.dir+getPath()的内容
getCanonicalPath()返回的就是标准的将符号完全解析的路径



                
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值