当前位置:   article > 正文

getPath()与getAbsolutePath()的区别_android getpath改用getabsolutepath

android getpath改用getabsolutepath

getPath()与getAbsolutePath()的区别

public void diff_pathAndAbsolutePath(){
File file1 = new File(“.\test1.txt”);
File file2 = new File(“D:\workspace\test\test1.txt”);
System.out.println(“—–默认相对路径:取得路径不同——”);
System.out.println(file1.getPath());
System.out.println(file1.getAbsolutePath());
System.out.println(“—–默认绝对路径:取得路径相同——”);
System.out.println(file2.getPath());
System.out.println(file2.getAbsolutePath());
}
得到的结果:

—–默认相对路径:取得路径不同——
.\test1.txt
D:\workspace\test.\test1.txt
—–默认绝对路径:取得路径相同——
D:\workspace\test\test1.txt
D:\workspace\test\test1.txt
结论:

getPath得到的是构造参数的路径。

getAbsolutePath得到的是全路径。如果构造参数是相对路径,则返回当前目录的绝对路径+构造参数路径;如果是绝对路径则直接返回

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/819880
推荐阅读
相关标签
  

闽ICP备14008679号