ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 10.1 输入和输出 可将Java库的IO类分割为输入与输出两个部分,这一点在用Web浏览器阅读联机Java类文档时便可知道。通过继承,从`InputStream`(输入流)派生的所有类都拥有名为`read()`的基本方法,用于读取单个字节或者字节数组。类似地,从`OutputStream`派生的所有类都拥有基本方法`write()`,用于写入单个字节或者字节数组。然而,我们通常不会用到这些方法;它们之所以存在,是因为更复杂的类可以利用它们,以便提供一个更有用的接口。因此,我们很少用单个类创建自己的系统对象。一般情况下,我们都是将多个对象重叠在一起,提供自己期望的功能。我们之所以感到Java的流库(Stream Library)异常复杂,正是由于为了创建单独一个结果流,却需要创建多个对象的缘故。 很有必要按照功能对类进行分类。库的设计者首先决定与输入有关的所有类都从`InputStream`继承,而与输出有关的所有类都从`OutputStream`继承。 ## 10.1.1 `InputStream`的类型 `InputStream`的作用是标志那些从不同起源地产生输入的类。这些起源地包括(每个都有一个相关的`InputStream`子类): (1) 字节数组 (2) `String`对象 (3) 文件 (4) “管道”,它的工作原理与现实生活中的管道类似:将一些东西置入一端,它们在另一端出来。 (5) 一系列其他流,以便我们将其统一收集到单独一个流内。 (6) 其他起源地,如Internet连接等(将在本书后面的部分讲述)。 除此以外,`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. ``` | 类 | 功能 | 构造器参数/如何使用 | | --- | --- | --- | | `PipedInputString` | 产生为相关的`PipedOutputStream`写的数据。实现了“管道化”的概念 | `PipedOutputStream`/作为一个数据源使用。通过将其同一个`FilterInputStream`对象连接,可提供一个有用的接口 | | `SequenceInputStream` | 将两个或更多的`InputStream`对象转换成单个`InputStream`使用 | 两个`InputStream`对象或者一个`Enumeration`,用于`InputStream`对象的一个容器/作为一个数据源使用。通过将其同一个`FilterInputStream`对象连接,可提供一个有用的接口 | | `FilterInputStream` | 对作为析构器接口使用的类进行抽象;那个析构器为其他`InputStream`类提供了有用的功能。参见表10.3 | 参见表10.3/参见表10.3 | ## 10.1.2 `OutputStream`的类型 这一类别包括的类决定了我们的输入往何处去:一个字节数组(但没有`String`;假定我们可用字节数组创建一个);一个文件;或者一个“管道”。 除此以外,`FilterOutputStream`为“析构器”类提供了一个基类,它将属性或者有用的接口同输出流连接起来。这将在以后讨论。 表10.2 `OutputStream`的类型 ``` 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 |