📜  Java中 getPath() 和 getCanonicalPath() 的区别

📅  最后修改于: 2022-05-13 01:55:21.617000             🧑  作者: Mango

Java中 getPath() 和 getCanonicalPath() 的区别

getPath()方法是 File 类的一部分。此函数返回给定文件对象的路径。该函数返回一个字符串对象,其中包含给定文件对象的路径,而getCanonicalPath()方法是 Path 类的一部分。此函数返回给定文件对象的规范路径名。如果文件对象的路径名是 Canonical,那么它只返回当前文件对象的路径。规范路径始终是绝对唯一的,该函数删除了“.”。 '..' 来自路径(如果存在)。现在,两种方法 getPath() 和 getCanonicalPath() 都是 File 类的一部分,并且都是获取文件系统路径的 File 方法。首先,我们从以下文件结构中了解两个路径之间的区别。

插图:考虑 windows 目录中的随机路径

|--D:
   \--Article      
   \--Test
   \--Program
       \--Python
       \--JAVA
           \Program1.java

如果路径-“ Python/../ Java/Program1. Java ”被传递然后传递路径和规范路径的值如下。

  • 路径: Python\..\ Java\Program1。Java
  • 规范路径: d:\Program\ Java\Program1。Java

现在让我们了解这两种方法之间的区别

(一)理论差异

getPath() MethodgetCanonicalPath() Method
This function returns the abstract pathname as a string.This function returns the Canonical pathname of the given file object.
The returned pathname is might be an absolute path.The canonical path is always an absolute and unique path. 
If String pathname is used to create a file object, it simply returns the pathname.This method first converts this pathname to absolute form if needed. To do that it will invoke the getAbsolutePath() Method and then maps it to its unique form.
If a URL is used in the argument of the file object then it removes the protocol and returns the file name This method removes the redundant names such as ” . “ and  “.. ” from the pathname. It will resolve the symbolic links and convert drive letters to a stander case.
This method is not going to throw any exceptions.

This method throws the following exceptions. 

Security Exception: if the required property value cannot be accessed.

I/O Exception: if I/O exception occurs.

Example:

File f = new File(“c:\\users\\..\\program\\.\\file1.txt”)

Path :- c:\users\..\program\.\file1.txt

Example:

File f = new File(“c:\\users\\..\\program\\.\\file1.txt”)

Canonical path :- c:\program\file1.txt

实施:(B)实际差异

例子

Java
// Java program to demonstrate the difference
// between getPath() and  getCanonicalPath() function
 
// Importing input output classes
import java.io.*;
 
// Class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Try block to check exceptions
        try {
 
            // Creating a file object
            File f = new File(
                "g:\\gfg\\A(7)PATH\\Test\\..\\file1.txt");
 
            // Getting the Path of file object
            String path = f.getPath();
 
            // Getting the Canonical path of the file object
            String canonical = f.getCanonicalPath();
 
            // Display the file path of the file object
            // and also the Canonical path of file
            System.out.println("Path : " + path);
            System.out.println("Canonical path : "
                               + canonical);
        }
 
        // Catch block to handle if exception/s occurs
        catch (Exception e) {
 
            // Exception message is printed where occurred
            System.err.println(e.getMessage());
        }
    }
}


输出:

Path (getPath()): g:\gfg\A(7)PATH\Test\..\file1.txt
path (getCanonicalPath()) : G:\gfg\A(7)PATH\file1.txt