The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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:本节包括以下内容:
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操作系统支持单个根节点,该节点由斜杠字符/
./
表示。
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, 在Microsoft Windows中,statusReport
is described by the following notation: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 (用于分隔目录名的字符(也称为分隔符)特定于文件系统:Solaris操作系统使用正斜杠(/
), and Microsoft Windows uses the backslash slash (\
)./
),Microsoft Windows使用反斜杠(\
)。
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
目录。
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
是链接的目标。
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 下一页将讨论Java编程语言中文件I/O支持的核心,即Path
class.Path
类。