什么是抽象路径?抽象路径名的意义在于程序逻辑编程中,直接以抽象路径代表不同系统下的路径,程序员不用考虑路径在不同系统的具体差异性。

抽象路径提供了一种系统无关的方式来表示文件和目录,确保程序员在不同操作系统下编程时无需关注路径的具体差异。在转换过程中,名称可以用默认或系统支持的分隔符分开。

英文解释如下:

  • User interfaces and operating systems use system-dependent pathname strings to name files and directories. This class presents an abstract, system-independent view of hierarchical pathnames.

  • When an abstract pathname is converted into a pathname string, each name is separated from the next by a single copy of the default separator character.

  • When a pathname string is converted into an abstract pathname, the names within it may be separated by the default name-separator character or by any other name-separator character that is supported by the underlying system.


对应的翻译如下:

用户界面和操作系统使用系统相关的路径名字符串来命名文件和目录。这个类提供了层次路径名的抽象的、系统独立的视图。

在将抽象路径名转换为路径名字符串时,每个名称都由一个默认分隔符的副本与下一个名称分隔开。

当将路径名字符串转换为抽象路径名时,可以使用默认的名称分隔符或底层系统支持的任何其他名称分隔符分隔其中的名称。


实践一下:

import java.io.File;
import java.io.IOException;
/*
 1.String getPath():将抽象路径名转换为路径名字符串;
 2.String getAbsolute():返回此抽象路径名的绝对路径名字符串;
 3.String getCanonicalPath():返回此抽象路径名的规范路径名字符串。
 */
public class FileDemo2 {
    public static void main(String[] args) throws IOException {
        // 传入目录相对路径
        File imagesDir = new File(".\\images");
        File imoocDir = new File("..\\imooc");

        // 传入文件相对路径
        File file = new File(".\\Hello.java");

        System.out.println("-- imagesDir ---");
        System.out.println(imagesDir.getPath());            //.\images
        System.out.println(imagesDir.getAbsolutePath());        //C:\mukew\Java_Tutorial\.\images
        System.out.println(imagesDir.getCanonicalPath());           //C:\mukew\Java_Tutorial\images

        System.out.println("-- imoocDir ---");
        System.out.println(imoocDir.getPath());             //..\imooc
        System.out.println(imoocDir.getAbsolutePath());         //C:\mukew\Java_Tutorial\..\imooc
        System.out.println(imoocDir.getCanonicalPath());            //C:\mukew\imooc

        System.out.println("-- file ---");
        System.out.println(file.getPath());                 //.\Hello.java
        System.out.println(file.getAbsolutePath());             //C:\mukew\Java_Tutorial\.\Hello.java
        System.out.println(file.getCanonicalPath());                //C:\mukew\Java_Tutorial\Hello.java
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值