getCanonicalPath()和getAbsolutePath()方法屬於Java中的java.io.File類。這些方法本質上用於獲取係統目錄結構中文件對象的路徑。
AbsoluteFilePath是文件對象的路徑名
- 如果我們使用抽象路徑創建文件對象,則絕對文件路徑與抽象文件路徑相同。
- 如果我們使用相對路徑創建文件對象,則絕對文件路徑是針對當前目錄解析相對路徑後獲得的路徑。
CanonicalFilePath是文件對象的路徑名
- 如果我們使用抽象路徑創建文件對象,則規範文件路徑與抽象文件路徑相同
- 如果我們使用相對路徑創建文件對象,則規範文件路徑即是最短的絕對路徑又是唯一路徑的路徑
讓我們舉個例子。說,我們使用路徑“ C:/folder1/folder2/folder3/file.txt”創建一個文件對象。顯然,上述路徑是抽象路徑,因此絕對文件路徑以及規範文件路徑都將與上述相同。
但是,如果在文件對象創建過程中提到的文件路徑類似於“ C:/folder1 /folder2 /folder3 /folder4 /../../folder3 /file.txt”,則絕對文件路徑將與上述路徑相同,但規範文件路徑將是最短的絕對路徑,即“ C:/folder1/folder2/folder3/file.txt”。
方法1:getAbsolutePath()
public String getAbsolutePath()
方法2:getCanonicalPath()
public String getCanonicalPath() throws IOException
例:
方法1:getAbsolutePath()
file.getAbsolutePath() // It returns absolute path of file object in system's directory structure
方法2:getCanonicalPath()
file.getCanonicalPath() // It returns canonical path of file object in system's directory structure
範例1:
Java
// Java Program to illustrate Difference Between
// getCanonicalPath() and getAbsolutePath()
// Importing input output classes
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
throws IOException
{
// Path of Test.txt is E:\workspace\gfg\Test.txt
// Test.txt is created inside project directory,
// Here project name is gfg
File file1 = new File("Test.txt");
// Getting th absolute path of the file
// using getAbsolutePath() method
System.out.println("Absolute Path:"
+ file1.getAbsolutePath());
// Getting the canonical path of the file
// using getCanonicalPath() method
System.out.println("Canonical Path:"
+ file1.getCanonicalPath());
}
}
輸出:
Absolute Path: E:\workspace\gfg\Test.txt Canonical Path:E:\workspace\gfg\Test.txt
輸出說明:在這裏,由於已經使用抽象的完整文件路徑創建了文件對象,因此這兩種方法都提供了與我們在文件對象創建期間使用的相同輸出。
範例2:
Java
// Java Program to illustrate Difference Between
// getCanonicalPath() and getAbsolutePath()
// Importing input output classes
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
throws IOException
{
// Creating an object of File class where
// Path of Test.txt is E:/workspace/gfg/Test.txt
File file2 = new File(
"e:\\workspace\\gfg\\..\\gfg\\Test.txt");
// Getting the absolute path of file
// using getAbsolutePath() method
System.out.println("Absolute Path:"
+ file2.getAbsolutePath());
// Getting the canonical path of file
// using getCanonicalPath() method
System.out.println("Canonical Path:"
+ file2.getCanonicalPath());
}
}
輸出:
Absolute Path: e:\workspace_2021-Apr\gfg\..\gfg\Test.txt Canonical Path:E:\workspace_2021-Apr\gfg\Test.txt
輸出說明:
這裏使用相對文件路徑創建了文件對象,該文件路徑本質上指向E:/workspace/gfg/Test.txt。因此,在調用getAbsolutePath()時,它提供了在創建文件對象時提到的相對路徑。但是在調用getCanonicalPath()時,它提供了抽象路徑或直接路徑,並將驅動器號轉換為標準大小寫。
範例3:
Java
// Java Program to illustrate Difference Between
// getCanonicalPath() and getAbsolutePath()
// Importing input output classes
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
throws IOException
{
// Now creating File class object where
// Path of Test.txt is E:/Test.txt
// Current Directory:E:/workspace/gfg/src
File file3 = new File("../../Test.txt");
// Getting the absolute path of file
// using getAbsolutePath()
System.out.println("Absolute Path:"
+ file3.getAbsolutePath());
// Getting the canonical path of file
// using getCanonicalPath()
System.out.println("Canonical Path:"
+ file3.getCanonicalPath());
}
}
輸出:
Absolute Path:E:\workspace\gfg\..\..\Test.txt Canonical Path:E:\Test.txt
輸出說明:
此處已創建具有完整相對路徑的文件對象。在調用getAbsolutePath()時,我們可以看到如何獲得路徑,而不是完全相對的路徑,而是部分相對的路徑。但是在調用getCanonicalPath()時,我們可以看到文件係統中的直接抽象路徑。
相關用法
- Java getPath()和getCanonicalPath()的區別用法及代碼示例
- Java getPath()和getAbsolutePath()的區別用法及代碼示例
- Java File getCanonicalPath()用法及代碼示例
- Java File getAbsolutePath()用法及代碼示例
- Java Scanner和BufferedReader的區別用法及代碼示例
- Java super()和this()的區別用法及代碼示例
- Java throw和throws的區別用法及代碼示例
- Java notify()和notifyAll()的區別用法及代碼示例
- Java Stream.of()和Arrays.stream()的區別用法及代碼示例
注:本文由純淨天空篩選整理自samandal2021大神的英文原創作品 Difference Between getCanonicalPath() and getAbsolutePath() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。