Documentation

The Java™ Tutorials
Hide TOC
What Is a Path? (And Other File System Facts)什么是路径(和其他文件系统事实)
Trail: Essential Java Classes
Lesson: Basic I/O
Section: File I/O (Featuring NIO.2)

What Is a Path? (And Other File System Facts)什么是路径?(和其他文件系统事实)

A file system stores and organizes files on some form of media, generally one or more hard drives, in such a way that they can be easily retrieved. 文件系统在某种形式的介质(通常是一个或多个硬盘驱动器)上存储和组织文件,以便轻松检索。Most file systems in use today store the files in a tree (or hierarchical) structure. 目前使用的大多数文件系统都以树(或层次)结构存储文件。At the top of the tree is one (or more) root nodes. Under the root node, there are files and directories (folders in Microsoft Windows). 树的顶部是一个(或多个)根节点。根节点下有文件和目录(Microsoft Windows中的文件夹)。Each directory can contain files and subdirectories, which in turn can contain files and subdirectories, and so on, potentially to an almost limitless depth.每个目录可以包含文件和子目录,而子目录又可以包含文件和子目录,等等,其深度几乎是无限的。

This section covers the following:本节包括以下内容:

What Is a Path?什么是路径?

The following figure shows a sample directory tree containing a single root node. 下图显示了包含单个根节点的示例目录树。Microsoft Windows supports multiple root nodes. Microsoft Windows支持多个根节点。Each root node maps to a volume, such as C:\ or D:\. 每个根节点映射到一个卷,例如C:\D:\The Solaris OS supports a single root node, which is denoted by the slash character, /.Solaris操作系统支持单个根节点,该节点由斜杠字符/表示。

Sample directory structure

Sample Directory Structure示例目录结构

A file is identified by its path through the file system, beginning from the root node. 文件由其在文件系统中的路径标识,从根节点开始。For example, the statusReport file in the previous figure is described by the following notation in the Solaris OS:例如,上图中的statusReport文件在Solaris操作系统中由以下符号描述:

/home/sally/statusReport

In Microsoft Windows, statusReport is described by the following notation:在Microsoft Windows中,statusReport由以下符号描述:

C:\home\sally\statusReport

The character used to separate the directory names (also called the delimiter) is specific to the file system: The Solaris OS uses the forward slash (/), and Microsoft Windows uses the backslash slash (\).用于分隔目录名的字符(也称为分隔符)特定于文件系统:Solaris操作系统使用正斜杠(/),Microsoft Windows使用反斜杠(\)。

Relative or Absolute?相对还是绝对?

A path is either relative or absolute. 路径可以是相对的,也可以是绝对的An absolute path always contains the root element and the complete directory list required to locate the file. 绝对路径始终包含根元素和定位文件所需的完整目录列表。For example, /home/sally/statusReport is an absolute path. 例如,/home/sally/statusReport是一个绝对路径。All of the information needed to locate the file is contained in the path string.定位文件所需的所有信息都包含在路径字符串中。

A relative path needs to be combined with another path in order to access a file. 为了访问文件,需要将相对路径与另一路径组合。For example, joe/foo is a relative path. 例如,joe/foo是一条相对路径。Without more information, a program cannot reliably locate the joe/foo directory in the file system.如果没有更多信息,程序就无法在文件系统中可靠地定位joe/foo目录。

Symbolic Links符号链接

File system objects are most typically directories or files. 文件系统对象通常是目录或文件。Everyone is familiar with these objects. 每个人都熟悉这些东西。But some file systems also support the notion of symbolic links. 但是一些文件系统也支持符号链接的概念。A symbolic link is also referred to as a symlink or a soft link.符号链接也称为符号链接软链接

A symbolic link is a special file that serves as a reference to another file. 符号链接是一个特殊文件,用作对另一个文件的引用。For the most part, symbolic links are transparent to applications, and operations on symbolic links are automatically redirected to the target of the link. 在大多数情况下,符号链接对应用程序是透明的,对符号链接的操作会自动重定向到链接的目标。(The file or directory being pointed to is called the target of the link.) (指向的文件或目录称为链接的目标。)Exceptions are when a symbolic link is deleted, or renamed in which case the link itself is deleted, or renamed and not the target of the link.例外情况是删除或重命名符号链接,在这种情况下,链接本身被删除,或重命名而不是链接的目标。

In the following figure, logFile appears to be a regular file to the user, but it is actually a symbolic link to dir/logs/HomeLogFile. 在下图中,logFile对用户来说似乎是一个常规文件,但实际上是指向dir/logs/HomeLogFile的符号链接。HomeLogFile is the target of the link.HomeLogFile是链接的目标。

Sample symbolic link

Example of a Symbolic Link.符号链接的示例。

A symbolic link is usually transparent to the user. 符号链接通常对用户是透明的。Reading or writing to a symbolic link is the same as reading or writing to any other file or directory.读取或写入符号链接与读取或写入任何其他文件或目录相同。

The phrase resolving a link means to substitute the actual location in the file system for the symbolic link. 短语解析链接意味着用文件系统中的实际位置替换符号链接。In the example, resolving logFile yields dir/logs/HomeLogFile.在本例中,解析logFile将生成dir/logs/HomeLogFile

In real-world scenarios, most file systems make liberal use of symbolic links. 在现实世界中,大多数文件系统都自由地使用符号链接。Occasionally, a carelessly created symbolic link can cause a circular reference. 偶尔,不小心创建的符号链接会导致循环引用。A circular reference occurs when the target of a link points back to the original link. 当链接的目标指向原始链接时,会发生循环引用。The circular reference might be indirect: directory a points to directory b, which points to directory c, which contains a subdirectory pointing back to directory a. 循环引用可能是间接的:目录a指向目录b,目录b指向目录c,目录c包含指向目录a的子目录。Circular references can cause havoc when a program is recursively walking a directory structure. 当程序递归遍历目录结构时,循环引用可能会造成严重破坏。However, this scenario has been accounted for and will not cause your program to loop infinitely.然而,这种情况已经得到了考虑,不会导致程序无限循环。

The next page discusses the heart of file I/O support in the Java programming language, the Path class.下一页将讨论Java编程语言中文件I/O支持的核心,即Path类。


Previous page: File I/O (Featuring NIO.2)
Next page: The Path Class