这是用户在 2024-9-30 16:42 为 https://edstem.org/au/courses/17312/lessons/55794/slides/377652 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?

10.2.1 Reader, writers and streams
10.2.1 读取器、写入器和流

Readers, Writers, and Streams

Files on a computing system generally come in two types. The more typical, which we are familiar with, are character-based files. These files contain data represented using characters. Some examples of these include HTML style web pages. These files store data using lines of characters and are generally human-readable. The second type of files are binary files. These files store data using 0s and 1s, and are not human readable. Most files that store data are binary files. Some examples of binary files are image files, sound files and executable files for various programs.
计算系统上的文件通常有两种类型。我们熟悉的更典型的是基于字符的文件。这些文件包含使用字符表示的数据。其中一些示例包括 HTML 样式的网页。这些文件使用字符行存储数据,并且通常是人类可读的。第二种类型的文件是二进制文件。这些文件使用 0 和 1 存储数据,并且不是人类可读的。存储数据的大多数文件都是二进制文件。二进制文件的一些示例包括各种程序的图像文件、声音文件和可执行文件。

The important distinction between character-based and binary files is that when programming we need to use the appropriate class to access, read, and/or write to these files. Character-based files use readers to read and writers to write. However, binary files use stream handlers. In this we will focus on character-based text files and work with readers and writers to access these files. But it is important to note that when working with files many different approaches exist and it is important to choose the most appropriate class to use, based on the requirements of the program.
基于字符的文件和二进制文件之间的重要区别在于,在编程时,我们需要使用适当的类来访问、读取和/或写入这些文件。基于字符的文件使用读取器进行读取,使用写入器进行写入。但是,二进制文件使用处理程序。在本文中,我们将重点介绍基于字符的文本文件,并与读取器和写入器一起访问这些文件。但需要注意的是,在处理文件时,存在许多不同的方法,根据程序的要求选择最合适的类非常重要。

Class libraries for file I/O
用于文件 I/O 的类库

The classes that perform file I/O can be found in the java․io and java.nio packages, and must be imported using the import command when files are used in a program. The former is a legacy package that has been part of the API from the beginning, while the latter is the new version of the package. Simply put, the main distinction between these packages is that the io package serves up data one byte at a time as a stream and doesn't support traversing the data in both directions. The nio package serves up chunks of data stored in buffers, which supports traversing the data in both directions.
执行文件 I/O 的类可以在 java.iojava.nio 包中找到,当在程序中使用文件时,必须使用 import 命令导入这些类。前者是从一开始就是 API 一部分的旧包,而后者是包的新版本。简单地说,这些包之间的主要区别在于 io 包一次以流的形式提供一个字节的数据,并且不支持双向遍历数据。nio 包提供存储在缓冲区中的数据块,它支持双向遍历数据。

At this point in time, we do not need to be concerned about using the nio package, but being aware of the developments in the language is important.
此时,我们不需要担心使用 nio 包,但了解语言的发展很重要。

For the rest of this lesson, we are going to focus on readers and writers only using the java․io package. The aim is to get you familiar with the process of reading and/or writing to basic text files.
在本课的其余部分,我们将重点介绍仅使用 java.io 包的读取器和写入器。目的是让您熟悉读取和/或写入基本文本文件的过程。