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) {}
	}
	}

能夠用上面代碼測試一下。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

相關文章
相關標籤/搜索