zoukankan      html  css  js  c++  java
  • file.getPath() getAbsolutePath() getCanonicalPath()区别

    package file;
    import java.io.File;
    import java.io.IOException;
     
    public class getFilePath {
     public static void main(String[] args) throws IOException
     {
         System.out.println("------默认相对路径,取得路径不同-----");
         File f = new File("..\src\file");
         System.out.println(f.getPath());
         System.out.println(f.getAbsolutePath());
         System.out.println(f.getCanonicalPath());
         System.out.println("------默认相对路径,取得路径不同-----");
         File f2 = new File(".\src\file");
         System.out.println(f2.getPath());
         System.out.println(f2.getAbsolutePath());
         System.out.println(f2.getCanonicalPath());
         System.out.println("------默认绝对路径,取得路径相同-----");
         File f3 = new File("C:\src\file");
         System.out.println(f3.getPath());
         System.out.println(f3.getAbsolutePath());
         System.out.println(f3.getCanonicalPath());
          
    //   执行结果为:
    //   ------默认相对路径,取得路径不同-----
    //   ..srcfile
    //   C:workspaceTip..srcfile
    //   C:workspacesrcfile
    //   ------默认相对路径,取得路径不同-----
    //   .srcfile
    //   C:workspaceTip.srcfile
    //   C:workspaceTipsrcfile
    //   ------默认绝对路径,取得路径相同-----
    //   C:srcfile
    //   C:srcfile
    //   C:srcfile
    //  
    //   比较可以得到
    //   getPath()返回的是构造方法里的路径,不做任何处理
    //   getAbsolutePath()返回的是 user.dir+getPath(),也就是执行路径加上构造方法中的路径
    //   getCanonicalPath()返回的是将符号完全解析的路径,也就是全路径
     }
    }
    
  • 相关阅读:
    linux常用命令
    linux下redis配置
    Git使用命令
    linux学习笔记
    NOPI读取Excel2003、Excel2007或更高级的兼容性问题
    netcore开发常用命令
    netcore3.0 dotnet ef执行报错
    vscode配置nuget常见问题
    PDMReader结合PowerDesigner导出word格式数据字典
    微信网页授权开发遇到问题
  • 原文地址:https://www.cnblogs.com/blog-cq/p/5954146.html
Copyright © 2011-2022 走看看