file的這幾個取得path的方法各有不同,下邊說說詳細的差別
概念上的差別:(内容來自jdk,個人感覺這個描述資訊,隻能讓明白的人明白,不明白的人看起來還是有點難度(特别試中文版,英文版稍好些)是以在概念之後我會舉例說明。如果感覺看概念很累就跳過直接看例子吧。看完例子回來看概念會好些。
file的getPath getAbsolutePath和getCanonicalPath的不同 getpath
file的getPath getAbsolutePath和getCanonicalPath的不同 public string getpath()将此抽象路徑名轉換為一個路徑名字元串。所得到的字元串使用預設名稱分隔符來分隔名稱序列中的名稱。
file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 傳回:
file的getPath getAbsolutePath和getCanonicalPath的不同 此抽象路徑名的字元串形式
file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 getabsolutepath
file的getPath getAbsolutePath和getCanonicalPath的不同 public string getabsolutepath()傳回抽象路徑名的絕對路徑名字元串。
file的getPath getAbsolutePath和getCanonicalPath的不同 如果此抽象路徑名已經是絕對路徑名,則傳回該路徑名字元串,這與 getpath() 方法一樣。如果此抽象路徑名是空的抽象路徑名,則傳回目前使用者目錄的路徑名字元串,該目錄由系統屬性 user.dir 指定。否則,使用與系統有關的方式分析此路徑名。在 unix 系統上,通過根據目前使用者目錄分析某一相對路徑名,可使該路徑名成為絕對路徑名。在 microsoft windows 系統上,通過由路徑名指定的目前驅動器目錄(如果有)來分析某一相對路徑名,可使該路徑名成為絕對路徑名;否則,可以根據目前使用者目錄來分析它。
file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 絕對路徑名字元串,它與此抽象路徑名表示相同的檔案或目錄的
file的getPath getAbsolutePath和getCanonicalPath的不同 抛出:
file的getPath getAbsolutePath和getCanonicalPath的不同 securityexception - 如果無法通路所需的系統屬性值。
file的getPath getAbsolutePath和getCanonicalPath的不同 另請參見:
file的getPath getAbsolutePath和getCanonicalPath的不同 isabsolute()
file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 getcanonicalpath
file的getPath getAbsolutePath和getCanonicalPath的不同 public string getcanonicalpath()
file的getPath getAbsolutePath和getCanonicalPath的不同 throws ioexception傳回抽象路徑名的規範路徑名字元串。
file的getPath getAbsolutePath和getCanonicalPath的不同 規範路徑名是絕對路徑名,并且是惟一的。規範路徑名的準确定義與系統有關。如有必要,此方法首先将路徑名轉換成絕對路徑名,這與調用 getabsolutepath() 方法的效果一樣,然後用與系統相關的方式将它映射到其惟一路徑名。這通常涉及到從路徑名中移除多餘的名稱(比如 "." 和 "..")、分析符号連接配接(對于 unix 平台),以及将驅動器名轉換成标準大小寫形式(對于 microsoft windows 平台)。
file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 表示現有檔案或目錄的每個路徑名都有一個惟一的規範形式。表示非存在檔案或目錄的每個路徑名也有一個惟一的規範形式。非存在檔案或目錄路徑名的規範形式可能不同于建立檔案或目錄之後同一路徑名的規範形式。同樣,現有檔案或目錄路徑名的規範形式可能不同于删除檔案或目錄之後同一路徑名的規範形式。
file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 表示與此抽象路徑名相同的檔案或目錄的規範路徑名字元串
file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 ioexception - 如果發生 i/o 錯誤(可能是因為構造規範路徑名需要進行檔案系統查詢)
file的getPath getAbsolutePath和getCanonicalPath的不同 securityexception - 如果無法通路所需的系統屬性值,或者存在安全管理器,且其 securitymanager.checkread(java.io.filedescriptor) 方法拒絕對該檔案進行讀取通路
file的getPath getAbsolutePath和getCanonicalPath的不同 從以下版本開始:
file的getPath getAbsolutePath和getCanonicalPath的不同 jdk1.1
file的getPath getAbsolutePath和getCanonicalPath的不同 二、例子:
1,getpath()與getabsolutepath()的差別
file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 public static void test1(){
file的getPath getAbsolutePath和getCanonicalPath的不同 file file1 = new file(".\\test1.txt");
file的getPath getAbsolutePath和getCanonicalPath的不同 file file2 = new file("d:\\workspace\\test\\test1.txt");
file的getPath getAbsolutePath和getCanonicalPath的不同 system.out.println("-----預設相對路徑:取得路徑不同------");
file的getPath getAbsolutePath和getCanonicalPath的不同 system.out.println(file1.getpath());
file的getPath getAbsolutePath和getCanonicalPath的不同 system.out.println(file1.getabsolutepath());
file的getPath getAbsolutePath和getCanonicalPath的不同 system.out.println("-----預設絕對路徑:取得路徑相同------");
file的getPath getAbsolutePath和getCanonicalPath的不同 system.out.println(file2.getpath());
file的getPath getAbsolutePath和getCanonicalPath的不同 system.out.println(file2.getabsolutepath());
file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 }
得到的結果:
file的getPath getAbsolutePath和getCanonicalPath的不同 -----預設相對路徑:取得路徑不同------
file的getPath getAbsolutePath和getCanonicalPath的不同 .\test1.txt
file的getPath getAbsolutePath和getCanonicalPath的不同 d:\workspace\test\.\test1.txt
file的getPath getAbsolutePath和getCanonicalPath的不同 -----預設絕對路徑:取得路徑相同------
file的getPath getAbsolutePath和getCanonicalPath的不同 d:\workspace\test\test1.txt
file的getPath getAbsolutePath和getCanonicalPath的不同 因為getpath()得到的是構造file的時候的路徑。
getabsolutepath()得到的是全路徑
如果構造的時候就是全路徑那直接傳回全路徑
如果構造的時候試相對路徑,傳回目前目錄的路徑+構造file時候的路徑
2,getabsolutepath()和getcanonicalpath()的不同
file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 public static void test2() throws exception
file的getPath getAbsolutePath和getCanonicalPath的不同 {
file的getPath getAbsolutePath和getCanonicalPath的不同 file file = new file("..\\src\\test1.txt");
file的getPath getAbsolutePath和getCanonicalPath的不同 system.out.println(file.getabsolutepath());
file的getPath getAbsolutePath和getCanonicalPath的不同 system.out.println(file.getcanonicalpath());
file的getPath getAbsolutePath和getCanonicalPath的不同 得到的結果
file的getPath getAbsolutePath和getCanonicalPath的不同 d:\workspace\test\..\src\test1.txt
file的getPath getAbsolutePath和getCanonicalPath的不同 d:\workspace\src\test1.txt
可以看到canonicalpath不但是全路徑,而且把..或者.這樣的符号解析出來。
3,getcanonicalpath()和自己的不同。
就是解釋這段話:
file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 單下邊這段代碼是看不到結果的,要配合一定的操作來看。下邊操作步驟,同時講解
file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 public static void test3() throws exception
file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 file file = new file("d:\\text.txt");
file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 步驟:
确定你的系統是windows系統。
(1),确定d盤下沒有text.txt這個檔案,直接執行這段代碼,得到的結果是:
file的getPath getAbsolutePath和getCanonicalPath的不同 d:\text.txt
注意這裡試大寫的text.txt
(2)在d盤下建立一個檔案,名叫text.txt,再次執行代碼,得到結果
file的getPath getAbsolutePath和getCanonicalPath的不同 同樣的代碼得到不同的結果。
同時可以對比getabsolutepath()看看,這個得到的結果是一樣的。
原因:
window是大小寫不敏感的,也就是說在windows上test.txt和test.txt是一個檔案,是以在windows上當檔案不存在時,得到的路徑就是按照輸入的路徑。但當檔案存在時,就會按照實際的情況來顯示。這也就是建立檔案後和删除檔案後會有不同的原因。檔案夾和檔案類似。
三、最後:
1,嘗試在linux下執行上邊的步驟,兩次列印的結果是相同的,因為linux是大小寫敏感的系統。
2,手動删掉test.txt,然後嘗試執行下邊代碼
file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 public static void test4() throws exception
file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 file file1 = new file("d:\\text.txt");
file的getPath getAbsolutePath和getCanonicalPath的不同 file1.createnewfile();
file的getPath getAbsolutePath和getCanonicalPath的不同 file = new file("d:\\text.txt");
file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 file的getPath getAbsolutePath和getCanonicalPath的不同 執行上邊兩個函數,看看結果,然後思考一下為什麼?
1,的結果是兩個大寫,
2,的結果試兩個小寫
連續兩個大寫的,是否跟上邊的沖突 ?
這是因為虛拟機的緩存機制造成的。第一次file file = new file("d:\\text.txt");決定了結果.