英文不好,拿来做阅读练习。。。。一步一步,有些地方可能表达有问题,见谅。

Overview of I/O Streams

i/o流的概述

To bring in information, a program opens a stream on an information source (a file, memory, a socket) and reads the information sequentially, as shown here:

为了获取信息,程序在信息源(文件,内存,socket)之间建立了一个流,然后按顺序读取信息,如下图所示:

!sequentially 这里怎么翻译不是很明白。

clip_image002

Similarly, a program can send information to an external destination by opening a stream to a destination and writing the information out sequentially, like this:

相似的过程,程序通过和外部的信息送达目标建立起一个流,然后把信息发送到目标,按顺序写出,像如下图一样:

clip_image004

No matter where the data is coming from or going to and no matter what its type, the algorithms for sequentially reading and writing data are basically the same:

无论数据从哪里来或者到哪里,也不管数据在那种介质中,按序读写数据的算法基本是相同的:

Reading

Writing

open a stream

while more information

read information

close the stream

open a stream

while more information

write information

close the stream

打开一个流

当有更多的数据信息时

读信息

关闭流

打开一个流

当有更多信息时候

写入信息

关闭流

 

The java.ioclip_image005 package contains a collection of stream classes that support these algorithms for reading and writing. To use these classes, a program needs to import the java.ioclip_image005[1] package. The stream classes are divided into two class hierarchies, based on the data type (either characters or bytes) on which they operate.

Java.io包中包含了一系列流的类,这些类支持读写操作的算法。为了使用这些类,程序中需要引入java.io包。这些流的类有2个分支,分支是基于他们操作时候的数据类型(是字符还是字节)。

clip_image007

Character Streams

字符流

Readerclip_image005[2] and Writerclip_image005[3] are the abstract superclasses for character streams in java.io. Reader provides the API and partial implementation for readers--streams that read 16-bit characters--and Writer provides the API and partial implementation for writers--streams that write 16-bit characters. Subclasses of Reader and Writer implement specialized streams and are divided into two categories: those that read from or write to data sinks (shown in gray in the following figures) and those that perform some sort of processing (shown in white). The figure shows the class hierarchies for the Reader and Writer classes.

Reader 和 Writer 是java.io包中那些字符流的抽象父类。Reader为读字符流提供了API和部分的实现--这些流读取16为的字符,Wirter为写字符流提供了API和部分的实现—这些流写入16为的字符。Reader和Writer的子类实现了专门的流,他们被分为2类:那些从节点读或者写入管道的类(在下图中显示为灰色)和那些执行了排序过程的类(显示为白色)。下面的图显示了Reader和Writer类们的层级关系。

clip_image009

Most programs should use readers and writers to read and write textual information. The reason is that they can handle any character in the Unicode character set, whereas the byte streams are limited to ISO-Latin-1 8-bit bytes.

很多程序应该使用字符读写类来读或写文本信息。因为他们可以处理任何Unicode编码的字符,而字节流却限制在处理ISO-Latin-1 8-bit字节上。

Byte Streams

字节流

To read and write 8-bit bytes, programs should use the byte streams, descendants of InputStreamclip_image005[4] and OutputStreamclip_image005[5]. InputStream and OutputStream provide the API and partial implementation for input streams (streams that read 8-bit bytes) and output streams (streams that write 8-bit bytes). These streams are typically used to read and write binary data such as images and sounds. Two of the byte stream classes, ObjectInputStream and ObjectOutputStream, are used for object serialization. These classes are covered in Object Serialization.

为了能够读写8位的字节,程序应当使用字节流,也就是InputStream和OutputStream的子类。InputStream和OutputStream为输入流(读8位字节的流)和输出流(写8位字节的流)提供了API和部分实现。这些流典型的用法是读写如图片和声音的二进制数据。有2中字节流,ObjectInputStream 和 ObjectOutputStream,被用在对象的序列化中。这些类被Serializetion类覆盖。

As with Reader and Writer, subclasses of InputStream and OutputStream provide specialized I/O that falls into two categories, as shown in the following class hierarchy figure: data sink streams (shaded) and processing streams (unshaded).

和Reader和Writer相比,InputStream和OutputStream用于处理专门I/O的子类也分为2类,如下图显示了他们的类层级:节点流(阴影的)和处理流(没有阴影的)

clip_image011

Understanding the I/O Superclasses

理解I/O的父类

Reader and InputStream define similar APIs but for different data types. For example, Reader contains these methods for reading characters and arrays of characters:

Reader 和 InputStream 定义了相似的API但是不同的数据类型。例如,Reader包含了这些方法用于读取字符和字符数组:

int read()

int read(char cbuf[])

int read(char cbuf[], int offset, int length)

InputStream defines the same methods but for reading bytes and arrays of bytes:

InputStream 定义了相同的方法但是用于读取字节和字节数组。

int read()

int read(byte cbuf[])

int read(byte cbuf[], int offset, int length)

Also, both Reader and InputStream provide methods for marking a location in the stream, skipping input, and resetting the current position.

相同的,Reader 和 InputStream都提供了方法用于标记流中的坐标,跳过输入,和重置当前坐标。

Writer and OutputStream are similarly parallel. Writer defines these methods for writing characters and arrays of characters:

Writer 和 OutputStream 也非常相似。Writer定义了这些方法用于写出字符和字符数组。

int write(int c)

int write(char cbuf[])

int write(char cbuf[], int offset, int length)

And OutputStream defines the same methods but for bytes:

OutputStream定义了这些相同的方法但是用于字节。

int write(int c)

int write(byte cbuf[])

int write(byte cbuf[], int offset, int length)

All of the streams--readers, writers, input streams, and output streams--are automatically opened when created. You can close any stream explicitly by calling its close method. Or the garbage collector can implicitly close it, which occurs when the object is no longer referenced.

所有的流-- readers, writers, input streams, and output streams—创建的时候会自动打开。你能通过close方法明确的关闭任何一个流。或者是垃圾回收期在暗中关闭,这种情况是发生在对象不再被引用的时候。