getCanonicalPath()
和getAbsolutePath()
方法属于Java的java.io.File
类。而这些方法基本上都是用来获取一个文件对象在系统目录结构中的路径。
AbsoluteFilePath是一个文件对象的路径名 -
如果使用抽象路径创建文件对象,那么绝对文件路径就与抽象文件路径相同。
如果使用相对路径来创建文件对象,那么绝对文件路径就是我们根据当前目录解析相对路径后得到的路径。
CanonicalFilePath是一个文件对象的路径名:
如果使用抽象路径创建文件对象,那么规范文件路径就与抽象文件路径相同。
如果使用相对路径创建文件对象,经典文件路径就是既是最短的绝对路径又是唯一路径的路径。
让我们举一个例子。例如,使用路径 "C:/folder1/folder2/folder3/file.txt"
创建一个文件对象。很明显,提到的路径是一个抽象的路径,所以绝对文件路径以及唯一文件路径都将与上述相同VSdIFfeR。
但如果在创建文件对象时提到的文件路径是 "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()
示例1:
// Java Program to illustrate Difference Between
// getCanonicalPath() and getAbsolutePath()
// Importing input output classes
import java.io.*;
// Main class
class YiibaiDemo {
// Main driver method
public static void main(String[] args)
throws IOException
{
// Here project name is yiibai
File file1 = new File("Test.txt");
System.out.println("Absolute Path: "+ file1.getAbsolutePath());
System.out.println("Canonical Path: "+ file1.getCanonicalPath());
}
}
运行结果:
Absolute Path: E:workspaceyiibaiTest.txt
Canonical Path: E:workspaceyiibaiTest.txt
输出解释: 在这里,由于文件对象已经被创建了一个抽象的、完整的文件路径,所以这两个方法都提供了与我们在创建文件对象时相同的输出
。例2:
import java.io.*;
// Main class
class YiibaiDemo {
// Main driver method
public static void main(String[] args)
throws IOException
{
File file2 = new File("e:workspaceyiibai..yiibaiTest.txt");
System.out.println("Absolute Path: "+ file2.getAbsolutePath());
System.out.println("Canonical Path: "+ file2.getCanonicalPath());
}
}
运行结果:
Absolute Path: e:workspace_2021-Apryiibai..gfgTest.txt
Canonical Path: E:workspace_2021-ApryiibaiTest.txt
输出解释:
这里的文件对象是使用相对文件路径创建的,基本上指向E:/workspace/gfg/Test.txt
。因此,在调用getAbsolutePath()
时,它提供了一个在创建文件对象时提到的相对路径。但在调用getCanonicalPath()
时,它提供了一个抽象路径或直接路径,并将盘符转换为标准大小写。
示例3:
import java.io.*;
// Main class
class YiibaiDemo {
// Main driver method
public static void main(String[] args)
throws IOException
{
File file3 = new File("../../Test.txt");
System.out.println("Absolute Path: "+ file3.getAbsolutePath());
System.out.println("Canonical Path: "+ file3.getCanonicalPath());
}
}
运行结果:
Absolute Path: E:workspaceyiibai....Test.txt
Canonical Path: E:Test.txt
这里的文件对象是用一个完整的相对路径创建的。在调用getAbsolutePath()
时,可以看到我们是如何得到路径的,不是完全的相对路径,而是部分相对路径 。但在调用getCanonicalPath()
时,可以看到文件系统中的直接抽象路径vsdiFFER。
以表格的形式来看看两者的区别:
- | getCanonicalPath() | getAbsolutePath() |
---|---|---|
1 | 这是一个存在于java.io文件类中的Java方法。 | 这是一个存在于java.io 文件类中的Java方法。 |
3 | 它不接受任何参数。 | 它不接受任何参数。 |
4 | 它返回表示与该抽象路径名相同的文件或目录的规范路径名字符串。 | 它返回一个字符串,表示绝对路径名字符串,表示与该抽象路径名相同的文件或目录。 |
5 | 与java 1.1兼容+与java 1.0兼容+。 |
Java 中 getCanonicalPath()和getAbsolutePath()的区别
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果 。
转载请注明:文章转载自 有区别网 [http://www.vsdiffer.com]
本文标题:Java 中 getCanonicalPath()和getAbsolutePath()的区别
本文链接:https://www.vsdiffer.com/vs/difference-between-getcanonicalpath-and-getabsolutepath-in-java.html
免责声明:以上内容仅代表 个人看法、理解、学习笔记、总结和研究收藏。不保证其正确性,因使用而带来的风险与本站无关!如本网站内容冒犯了您的权益,请联系站长,邮箱:,我们核实并会尽快处理。