脚本宝典收集整理的这篇文章主要介绍了java File 的getPath getAbsolutePath和getCanonicalPath的不同,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
最近在做安卓作业的时候遇到了getcanonicalPath,遂上网查了一番资料
来看一个DEMO吧:
public class test_path { public static void main(String[] args) throws IOException{ File file1 = new File("..\test1.txt"); File file2 = new File("D:\workspace\test\test1.txt"); System.out.PRintln("-----默认相对路径:取得路径不同------"); System.out.println("getPath(): "+file1.getPath()); System.out.println("getAbsolutePath(): "+file1.getAbsolutePath()); System.out.println("getCanonicalPath(): "+file1.getCanonicalPath()); System.out.println("-----默认绝对路径:取得路径相同------"); System.out.println("getPath(): "+file2.getPath()); System.out.println("getAbsolutePath(): "+file2.getAbsolutePath()); System.out.println("getCanonicalPath(): "+file2.getCanonicalPath()); } }
这是输出结果:
-----默认相对路径:取得路径不同------ getPath(): ..test1.txt getAbsolutePath(): D:DevelopCodeJAVA-itcasttest_path..test1.txt getCanonicalPath(): D:DevelopCodeJAVA-ITcasttest1.txt -----默认绝对路径:取得路径相同------ getPath(): D:workspacetesttest1.txt getAbsolutePath(): D:workspacetesttest1.txt getCanonicalPath(): D:workspacetesttest1.txt
让我们总结一下吧:
函数名 绝对路径 相对路径 getPath() 就是构造file的时候的路径 就是file字符串 getAbsolutePath() 返回当前目录的路径+构造file时候的路径 getCanonicalPath() 在getAbsolutePath()基础上把..或.这样的符号解析出来
以上是脚本宝典为你收集整理的java File 的getPath getAbsolutePath和getCanonicalPath的不同全部内容,希望文章能够帮你解决java File 的getPath getAbsolutePath和getCanonicalPath的不同所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。