Java与pathon,Java getPath()和getCononicalPath()的区别用法及代码示例

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

getPath()是URL类的一种方法,它将给定的抽象路径名转换为路径名字符串。结果字符串使用默认的name-separator字符分隔名称序列中的名称。

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

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());

}

}

输出:

176253ed66a8f473f33d1d02185eb3c9.png

getAbsolutePath()返回表示给定路径的绝对路径的路径对象。如果给定的路径名​​已经是绝对的,则仅通过getPath()方法返回路径名字符串。如果此抽象路径名是空的抽象路径名,则返回由系统属性用户“ .dir”()命名的当前用户目录的路径名字符串。否则,将以system-dependent的方式解析此路径名。

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

On Unix’s System:通过针对当前用户目录解析相对路径名,可以使该路径为绝对路径。

On Microsoft System:通过将相对路径名与由路径名命名的驱动器的当前目录(如果有)解析,可以使它成为绝对路径。如果不是,则针对当前用户目录进行解析。

例:

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());

}

}

输出:

82f847ece0ccec8f2e9a3a4dda77c88f.png

此方法返回给定抽象路径名的规范路径名字符串。规范路径名是绝对的,也是唯一的。如有必要,此方法首先将路径名转换为绝对格式,就像调用getAbsolutePath()方法一样,然后以system-dependent方式将其映射为唯一格式。

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

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 argumennts

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

// Print an display the the canonical path

// name string

System.out.println("Output:"

+ path.getCanonicalPath());

}

}

输出:

23dff460feae7157817bf63ec95c87a5.png

输出说明:为了更好地解释这些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()

此方法返回一个字符串,该字符串表示文件对象表示的文件的(绝对或相对)路径名。

此方法返回抽象文件路径名的绝对路径名字符串。

此方法返回给定抽象路径名的规范路径名字符串。

如果使用相对路径创建文件对象,则返回的路径是相对路径。

如果抽象路径名是相对的,则以system-dependent的方式解析。

如果文件对象是使用相对路径创建的,则此方法首先将路径名转换为绝对路径并将其映射为唯一形式。

如果使用绝对路径创建文件对象,则返回的路径为绝对路径。

如果抽象路径名已经是绝对路径,则返回相同的路径名字符串。

如果使用相对路径创建文件对象,则返回的路径将采用唯一形式。

此方法不解析路径名。

此方法仅解析当前目录的相对路径。速记表示形式(例如“.”和“..”)不再得到解决。

此方法涉及从路径名中删除冗余名称(例如“.”和“..”),解析符号链接(在Unix平台上)以及将驱动器号转换为标准大小写(在Microsoft window平台上)。

在视窗系统上

文件路径=新文件(“Notes/../demo.txt”);

输出:

Notes \ .. \ demo.txt

在Unix系统上

文件路径=新文件(“ Notes /../demo.txt”)

输出:

注释/../demo.txt

视窗系统

文件路径=新文件(“Notes/../demo.txt”);

输出:

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

在Unix系统上

文件路径=新文件(“ Notes /../demo.txt”)

输出:

主页/pooja /桌面/便笺/../demo.txt

在Window系统上

文件路径=新文件(“Notes/../demo.txt”);

输出:

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

在Unix系统上

文件路径=新文件(“ Notes /../demo.txt”)

输出:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值