Documentation

The Java™ Tutorials
Hide TOC
I/O StreamsI/O流
Trail: Essential Java Classes
Lesson: Basic I/O

I/O Streams

An I/O Stream represents an input source or an output destination.I/O流表示输入源或输出目标。A stream can represent many different kinds of sources and destinations, including disk files, devices, other programs, and memory arrays.流可以表示许多不同类型的源和目标,包括磁盘文件、设备、其他程序和内存阵列。

Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and objects.流支持多种不同类型的数据,包括简单字节、基本数据类型、本地化字符和对象。Some streams simply pass on data; others manipulate and transform the data in useful ways.有些流只是传递数据;其他人则以有用的方式操作和转换数据。

No matter how they work internally, all streams present the same simple model to programs that use them: A stream is a sequence of data.无论它们在内部如何工作,所有流都向使用它们的程序呈现相同的简单模型:流是数据序列。A program uses an input stream to read data from a source, one item at a time:程序使用输入流从源读取数据,每次读取一项:

Reading information into a program.

Reading information into a program.将信息读入程序。

A program uses an output stream to write data to a destination, one item at time:程序使用输出流将数据写入目标,每次写入一项:

Writing information from a program.

Writing information from a program.从程序中写入信息。

In this lesson, we'll see streams that can handle all kinds of data, from primitive values to advanced objects.在本课程中,我们将看到可以处理各种数据的流,从基本值到高级对象。

The data source and data destination pictured above can be anything that holds, generates, or consumes data.上图中的数据源和数据目的地可以是保存、生成或使用数据的任何东西。Obviously this includes disk files, but a source or destination can also be another program, a peripheral device, a network socket, or an array.显然,这包括磁盘文件,但源或目标也可以是另一个程序、外围设备、网络套接字或阵列。

In the next section, we'll use the most basic kind of streams, byte streams, to demonstrate the common operations of Stream I/O.在下一节中,我们将使用最基本的流类型字节流来演示流I/O的常见操作。For sample input, we'll use the example file xanadu.txt, which contains the following verse:对于示例输入,我们将使用示例文件xanadu.txt,其中包含以下内容:

In Xanadu did Kubla Khan
A stately pleasure-dome decree:
Where Alph, the sacred river, ran
Through caverns measureless to man
Down to a sunless sea.

Previous page: Basic I/O
Next page: Byte Streams