该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” 。
方法一: getAbsolutePath()
public String getAbsolutePath()
方法二: getCanonicalPath()
public String getCanonicalPath() throws IOException
例子:
方法一: getAbsolutePath()
file.getAbsolutePath()
// It returns absolute path of file object in system's directory structure
方法二: 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());
}
}
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());
}
}
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:\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() 时,我们可以看到文件系统中的直接抽象路径。