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

来源:互联网 发布:ftp安卓软件 编辑:程序博客网 时间:2024/05/16 17:58
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.htmlFile 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()返回的就是标准的将符号完全解析的路径


                                             
0 0