📜  JavagetPath() 和 getCononicalPath() 的区别

📅  最后修改于: 2021-09-16 10:26:23             🧑  作者: Mango

在讨论差异之前,让我们快速回顾一下所有三种方法

  1. 获取路径() 方法
  2. 获取绝对路径() 方法
  3. getCanonicalPath()方法

1.获取路径() 方法

获取路径() 是 URL 类的一个方法。它将给定的抽象路径名转换为路径名字符串。结果字符串使用默认名称分隔字符来分隔名称序列中的名称。

返回:抽象路径名的字符串形式

例子

Java
// Java Program illustrating the getPath() method
 
// Importing input output classes
import java.io.*;
 
// Class for getPath method()
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a new file onject in which input is
        // absoute path as in argument from the user
        File path1 = new File(
            "C:/Users/ASPIRE/Desktop/Java/Notes/Chapter one/demo.txt");
 
        // Print the display the path string for
        // absolute path using getPath() method
        System.out.println(
            "Output for given absolute path : "
            + path1.getPath());
 
        // Creating another object of File and this time
        // relative path is provided as an input
        File path2 = new File("Notes/../demo.txt");
 
        // Print the display the path string for
        // relative path using getPath() method
        System.out.println(
            "Output for given relative path : "
            + path2.getPath());
    }
}


Java
// Java Program illustrating the getAbsolutePath() Method
 
// Importing all input output classes
import java.io.*;
 
// Class to get getAbsolutePath
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating a file object where
        // relatie path is provided as in paarameter
        File path = new File("Notes/../demo.txt");
 
        // Print and display the string representing
        // absolute path of the file
        System.out.println("Output : "
                           + path.getAbsolutePath());
    }
}


Java
// Java Program illustrating the getCanonicalPath() method
 
// Importing all input output classes
import java.io.*;
 
// Class for showcasing getCanonicalPath method
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
        // Creating a new File object and
        // providing it relative path as in arguments
        File path = new File("Notes/../demo.txt");
 
        // Print an display the the canonical path
        // name string
        System.out.println("Output: "
                           + path.getCanonicalPath());
    }
}


输出:

2. 获取绝对路径() 方法

getAbsolutePath()返回一个路径对象,表示给定路径的绝对路径。如果给定的路径名已经是绝对的,那么路径名字符串将像getPath()方法一样简单地返回。如果此抽象路径名是空抽象路径名,则返回当前用户目录的路径名字符串,由系统属性 user ‘.dir'() 命名。否则此路径名以系统相关的方式解析。

返回:表示与此抽象路径名相同的文件或目录的绝对路径名字符串

  • 在 Unix 系统上:相对路径名通过针对当前用户目录进行解析而成为绝对路径。
  • 在 Microsoft 系统上:通过针对由路径名命名的驱动器的当前目录(如果有)进行解析,从而使相对路径名成为绝对路径。如果不是,则针对当前用户目录进行解析。

例子:

Java

// Java Program illustrating the getAbsolutePath() Method
 
// Importing all input output classes
import java.io.*;
 
// Class to get getAbsolutePath
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating a file object where
        // relatie path is provided as in paarameter
        File path = new File("Notes/../demo.txt");
 
        // Print and display the string representing
        // absolute path of the file
        System.out.println("Output : "
                           + path.getAbsolutePath());
    }
}

输出:

3 . getCanonicalPath()方法

此方法返回给定抽象路径名的规范路径名字符串。规范路径名既是绝对的又是唯一的。如有必要,此方法首先将路径名转换为绝对形式,就像调用getAbsolutePath()方法一样,然后以依赖于系统的方式将其映射到其唯一形式。

返回:表示与抽象路径名相同的文件或目录的规范路径名字符串。

Java

// Java Program illustrating the getCanonicalPath() method
 
// Importing all input output classes
import java.io.*;
 
// Class for showcasing getCanonicalPath method
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
        // Creating a new File object and
        // providing it relative path as in arguments
        File path = new File("Notes/../demo.txt");
 
        // Print an display the the canonical path
        // name string
        System.out.println("Output: "
                           + path.getCanonicalPath());
    }
}

输出:

输出说明:为了更好地解释这些 CMD 输出或硬编码输出,使用的Java文件的指定位置如下

:C:\Users\ASPIRE\Desktop\Java\getPathExample or getAbsoltePathExample or getCanonicalPathExample

demo.txt 文件的位置

:C:\Users\ASPIRE\Desktop\Java\Notes\Chapter one\demo.txt

现在,在讨论了它们中的每一个之后,让我们深入探讨getPath()getAbsolutePath() getCanonicalPath()之间的差异 如下:

               getPath()                                                                                getAbsolutePath()                                                                                                                                       getCononicalPath()                         
This method returns a string that denotes (absolute or relative) pathname of the file represented by the file object. This method returns the absolute pathname string of the abstract file pathname. This method returns the canonical pathname string of the given abstract pathname.
If the file object is created using a relative path then the path returned is a relative path. If the abstract pathname is relative, then it is resolved in a system-dependent way. If the file object is created using a relative path then this method first converts pathname to absolute and maps it to a unique form. 
If the file object is created using the absolute path then the path returned is the absolute path. If the abstract pathname is already absolute, then the same path name string is returned. If the file object is created using a relative path then the path returned will be in unique form.
This method does not resolve pathname. This method only resolves the current directory for a relative path. Shorthand representations (such as “.” and “..”) are not resolved further.                        This method involves removing redundant names such as “.” and “..” from the pathname, resolving symbolic links (On Unix platform), and converting drive letters to a standard case (On Microsoft Windows platform).

Example

On window’s System

File path= new File(“Notes/../demo.txt”);  

Output:

Notes\..\demo.txt

On Unix’s System

File path = new File(“Notes/../demo.txt “)

Output:

Notes/../demo.txt

Example

Window’s System

File path= new File(“Notes/../demo.txt”);                    

Output:

C:\Users\ASPIRE\Desktop\Java\Notes\..\demo.txt  

On Unix’s System

File path = new File(“Notes/../demo.txt “)

Output:

home/pooja/Desktop/Notes/../demo.txt

Example

On Window’s System

File path= new File(“Notes/../demo.txt”);

Output:

C:\Users\ASPIRE\Desktop\Java\demo.txt

On Unix’s System

File path = new File(“Notes/../demo.txt “)

Output:

/home/pooja/Desktop/Java/demo.txt