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

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()中间的相对路径进行转换。


你可能感兴趣的:(getPath())