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) {} } }
能够用上面代码测试一下。java
Your output will be something like:ide
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 参数。
spa
getAbsolutePath()
gets the path string after resolving it against the current directory if it's relative, resulting in a fully qualified path.code
总结来看:getAbsolutePath() 是得到绝对路径,可是不对getPath里面的相对路径进行转换。orm
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()中间的相对路径进行转换。rem