“对语言设计人员来说,创建好的输入/输出系统是一项特别困难的任务。”
由于存在大量不同的设计方案,所以该任务的困难性是很容易证明的。其中最大的挑战似乎是如何覆盖所有可能的因素。不仅有三种不同的种类的IO需要考虑(文件、控制台、网络连接),而且需要通过大量不同的方式与它们通信(顺序、随机访问、二进制、字符、按行、按字等等)。
Java库的设计者通过创建大量类来攻克这个难题。事实上,Java的IO系统采用了如此多的类,以致刚开始会产生不知从何处入手的感觉(具有讽刺意味的是,Java的IO设计初衷实际要求避免过多的类)。从Java1.0升级到Java 1.1后,IO库的设计也发生了显著的变化。此时并非简单地用新库替换旧库,Sun的设计人员对原来的库进行了大手笔的扩展,添加了大量新的内容。因此,我们有时不得不混合使用新库与旧库,产生令人无奈的复杂代码。
本章将帮助大家理解标准Java库内的各种IO类,并学习如何使用它们。本章的第一部分将介绍“旧”的Java 1.0 IO流库,因为现在有大量代码仍在使用那个库。本章剩下的部分将为大家引入Java 1.1 IO库的一些新特性。注意若用Java 1.1编译器来编译本章第一部分介绍的部分代码,可能会得到一条“不建议使用该特性”(Deprecated feature)警告消息。代码仍然能够使用;编译器只是建议我们换用本章后面要讲述的一些新特性。但我们这样做是有价值的,因为可以更清楚地认识老方法与新方法之间的一些差异,从而加深我们的理解(并可顺利阅读为Java 1.0写的代码)。很有必要按照功能对类进行分类。库的设计者首先决定与输入有关的所有类都从InputStream继承,而与输出有关的所有类都从OutputStream继承。
InputStream的作用是标志那些从不同起源地产生输入的类。这些起源地包括(每个都有一个相关的InputStream子类):
除此以外,FilterInputStream也属于InputStream的一种类型,用它可为“破坏器”类提供一个基础类,以便将属性或者有用的接口同输入流连接到一起。这将在以后讨论。
Class | Function | Constructor Arguments | How to use it |
---|---|---|---|
ByteArray-InputStream | Allows a buffer in memory to be used as an InputStream. | The buffer from which to extract the bytes. | As a source of data. Connect it to a FilterInputStream object to provide a useful interface. |
StringBuffer-InputStream | Converts a String into an InputStream. | A String. The underlying implementation actually uses a StringBuffer. | As a source of data. Connect it to a FilterInputStream object to provide a useful interface. |
File-InputStream | For reading information from a file. | A String representing the file name, or a File or FileDescriptor object. | As a source of data. Connect it to a FilterInputStream object to provide a useful interface. |
类 功能 构建器参数/如何使用
ByteArrayInputStream 允许内存中的一个缓冲区作为InputStream使用从中提取字节的缓冲区/作为一个数据源使用。通过将其同一个FilterInputStream对象连接,可提供一个有用的接口
StringBufferInputStream 将一个String转换成InputStream 一个String(字串)。基础的实施方案实际采用一个StringBuffer(字串缓冲)/作为一个数据源使用。通过将其同一个FilterInputStream对象连接,可提供一个有用的接口
FileInputStream 用于从文件读取信息 代表文件名的一个String,或者一个File或FileDescriptor对象/作为一个数据源使用。通过将其同一个FilterInputStream对象连接,可提供一个有用的接口
Piped-InputStream | Produces the data that’s being written to the associated PipedOutput-Stream. Implements the “piping” concept. | PipedOutputStream | As a source of data in multithreading. Connect it to a FilterInputStream object to provide a useful interface. |
Sequence-InputStream | Coverts two or more InputStream objects into a single InputStream. | Two InputStream objects or an Enumeration for a container of InputStream objects. | As a source of data. Connect it to a FilterInputStream object to provide a useful interface. |
Filter-InputStream | Abstract class which is an interface for decorators that provide useful functionality to the other InputStream classes. See Table 10-3. | See Table 10-3. | See Table 10-3. |
PipedOutputStream/作为一个数据源使用。通过将其同一个FilterInputStream对象连接,可提供一个有用的接口
SequenceInputStream 将两个或更多的InputStream对象转换成单个InputStream使用两个InputStream对象或者一个Enumeration,用于InputStream对象的一个容器/作为一个数据源使用。通过将其同一个FilterInputStream对象连接,可提供一个有用的接口
FilterInputStream 对作为破坏器接口使用的类进行抽象;那个破坏器为其他InputStream类提供了有用的功能。参见表10.3参见表10.3/参见表10.3
这一类别包括的类决定了我们的输入往何处去:一个字节数组(但没有String;假定我们可用字节数组创建一个);一个文件;或者一个“管道”。
除此以外,FilterOutputStream为“破坏器”类提供了一个基础类,它将属性或者有用的接口同输出流连接起来。这将在以后讨论。
Class | Function | Constructor Arguments | How to use it |
---|---|---|---|
ByteArray-OutputStream | Creates a buffer in memory. All the data that you send to the stream is placed in this buffer. | Optional initial size of the buffer. | To designate the destination of your data. Connect it to a FilterOutputStream object to provide a useful interface. |
File-OutputStream | For sending information to a file. | A String representing the file name, or a File or FileDescriptor object. | To designate the destination of your data. Connect it to a FilterOutputStream object to provide a useful interface. |
Piped-OutputStream | Any information you write to this automatically ends up as input for the associated PipedInput-Stream. Implements the “piping” concept. | PipedInputStream | To designate the destination of your data for multithreading. Connect it to a FilterOutputStream object to provide a useful interface. |
Filter-OutputStream | Abstract class which is an interface for decorators that provide useful functionality to the other OutputStream classes. See Table 10-4. | See Table 10-4. | See Table 10-4. |
类 功能 构建器参数/如何使用
ByteArrayOutputStream 在内存中创建一个缓冲区。我们发送给流的所有数据都会置入这个缓冲区。可选缓冲区的初始大小/用于指出数据的目的地。若将其同FilterOutputStream对象连接到一起,可提供一个有用的接口
FileOutputStream 将信息发给一个文件 用一个String代表文件名,或选用一个File或FileDescriptor对象/用于指出数据的目的地。若将其同FilterOutputStream对象连接到一起,可提供一个有用的接口
PipedOutputStream 我们写给它的任何信息都会自动成为相关的PipedInputStream的输出。实现了“管道化”的概念PipedInputStream/为多线程处理指出自己数据的目的地/将其同FilterOutputStream对象连接到一起,便可提供一个有用的接口
FilterOutputStream 对作为破坏器接口使用的类进行抽象处理;那个破坏器为其他OutputStream类提供了有用的功能。参见表10.4参见表10.4/参见表10.4