英文解释如下:
-
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
}
}

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

813

被折叠的 条评论
为什么被折叠?



