您的位置:首页 > 编程语言 > PHP开发

getPath() getAbsolutePath() getCanonicalPath()不同之处

2016-04-26 00:00 561 查看
摘要: What's the difference between getPath(), getAbsolutePath(), and getCanonicalPath() in Java?

import java.io.File;public class PathTesting {
public static void main(String [] args) {
File f = new File("test/.././file.txt");
System.out.println(f.getPath());
System.out.println(f.getAbsolutePath());
try {
System.out.println(f.getCanonicalPath());
}
catch(Exception e) {}
}
}

可以用上面代码测试一下。

Your output will be something like:
test\..\.\file.txt
C:\projects\sandbox\trunk\test\..\.\file.txt
C:\projects\sandbox\trunk\file.txt


getPath()
gets the path string that the
File
object was constructed with, and it may be relative current directory.
总结来看:getPath() 是 获得这个File对象所传入的path 参数。

getAbsolutePath()
gets the path string after resolving it against the current directory if it's relative, resulting in a fully qualified path.
总结来看:getAbsolutePath() 是获得绝对路径,但是不对getPath里面的相对路径进行转换。

getCanonicalPath()
gets the path string after resolving any relative path against current directory, and removes any relative pathing (
.
and
..
), and any file system links to return a path which the file system considers the canonical means to reference the file system object to which it points.
总结来看:getCanonicalPath() 是对getAbsolutePath()中间的相对路径进行转换。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息