Documentation

The Java™ Tutorials
Hide TOC
The Path Class路径类
Trail: Essential Java Classes
Lesson: Basic I/O
Section: File I/O (Featuring NIO.2)

The Path Class路径类

The Path class, introduced in the Java SE 7 release, is one of the primary entrypoints of the java.nio.file package. JavaSE7版本中引入的Path类是java.nio.file包的主要入口点之一。If your application uses file I/O, you will want to learn about the powerful features of this class.如果您的应用程序使用文件I/O,则需要了解此类的强大功能。


Version Note:版本说明: If you have pre-JDK7 code that uses java.io.File, you can still take advantage of the Path class functionality by using the File.toPath method. 如果您有使用java.io.File的JDK7之前的代码,那么仍然可以通过使用File.toPath方法来利用Path类的功能。See Legacy File I/O Code for more information. 有关更多信息,请参阅遗留文件I/O代码

As its name implies, the Path class is a programmatic representation of a path in the file system. 顾名思义,Path类是文件系统中路径的编程表示。A Path object contains the file name and directory list used to construct the path, and is used to examine, locate, and manipulate files.Path对象包含用于构造路径的文件名和目录列表,并用于检查、定位和操作文件。

A Path instance reflects the underlying platform. Path实例反映了底层平台。In the Solaris OS, a Path uses the Solaris syntax (/home/joe/foo) and in Microsoft Windows, a Path uses the Windows syntax (C:\home\joe\foo). 在Solaris操作系统中,路径使用Solaris语法(/home/joe/foo),而在Microsoft Windows中,路径使用Windows语法(C:\home\joe\foo)。A Path is not system independent. Path不是独立于系统的。You cannot compare a Path from a Solaris file system and expect it to match a Path from a Windows file system, even if the directory structure is identical and both instances locate the same relative file.即使目录结构相同且两个实例都位于相同的相对文件中,也不能比较Solaris文件系统中的Path并期望它与Windows文件系统中的Path匹配。

The file or directory corresponding to the Path might not exist. Path对应的文件或目录可能不存在。You can create a Path instance and manipulate it in various ways: you can append to it, extract pieces of it, compare it to another path. 您可以创建一个Path实例,并以各种方式对其进行操作:可以附加到它,提取它的片段,将它与另一个路径进行比较。At the appropriate time, you can use the methods in the Files class to check the existence of the file corresponding to the Path, create the file, open it, delete it, change its permissions, and so on.在适当的时候,可以使用Files类中的方法检查路径对应的文件是否存在、创建文件、打开文件、删除文件、更改其权限等。

The next page examines the Path class in detail.下一页将详细检查Path类。


Previous page: What Is a Path? (And Other File System Facts)
Next page: Path Operations