getPath vs getAbsolutePath vs getCanonicalPath

作者 : 码海拾光 本文共952个字,预计阅读时间需要3分钟 发布时间: 2023-02-28 共490人阅读




getPath vs getAbsolutePath vs getCanonicalPath

getPath vs getAbsolutePath vs getCanonicalPath

  1. getPath

    以构造路径作为返回值

  2. getAbsolutePath

    以绝对路径作为返回值

  3. getCanonicalPath

    以绝对路径作为返回值(如果路径包含.或..会进行处理)

public static void main(String[] args){
	  File file1 = new File(".\\xxx.txt");
    File file2 = new File("D:\\aaa\\xxx.txt");

    System.out.println(file1.getPath());
    System.out.println(file1.getAbsolutePath());
	  System.out.println(file1.getCanonicalPath());
    System.out.println("------------------------");
    System.out.println(file2.getPath());
    System.out.println(file2.getAbsolutePath());
	  System.out.println(file2.getCanonicalPath());
}
 
 
输出如下内容:

.\xxx.txt
D:\aaa\.\xxx.txt
D:\aaa\xxx.txt
------------------------
D:\aaa\xxx.txt
D:\aaa\xxx.txt
D:\aaa\xxx.txt

系统推荐



  • 随机毒鸡汤:所谓婚姻,就是两个家庭的,资产重组。

    Captivating winter landscape of snowy Dents du Midi in Val-d'Illiez, Switzerland.


文章来源于justsoso.fun: getPath vs getAbsolutePath vs getCanonicalPath





发表回复