简体   繁体   English

Java I / O流定义和子类型

[英]Java I/O stream definition and subtypes

I'm new to I/O in Java, and read in one of the posts on this site that: 我是Java I / O的新手,并阅读了该网站上的一篇文章:

All streams behave in the same manner, even if the actual physical devices to which they are linked differ. 即使它们链接到的实际物理设备不同,所有流的行为也相同。 Thus, the same I/O classes and methods can be applied to any type of device 因此,可以将相同的I / O类和方法应用于任何类型的设备

Quoted from: Stream definition 引用自: 流定义

What I can't wrap my head around is how is it that all streams (take the different byte stream subtypes for example - BufferedInputStream , FilterInputStream , ObjectInputStream , .., etc.) behave in the same manner and can be connected to any physical device, when they are implemented as different classes to supposedly offer varying functionality and accomodate different sources/destinations? 我无法确定的是,所有流(以不同的字节流子类型为例-BufferedInputStreamFilterInputStreamObjectInputStream等)的行为方式相同,并且可以连接到任何物理端口设备,何时将它们实现为不同的类以提供不同的功能并适应不同的源/目的地? For example, can I use ObjectInputStream or FileOutputStream to read from and write to the console? 例如,我可以使用ObjectInputStreamFileOutputStream来读取和写入控制台吗? Different streams, different devices, and all (streams) can be connected to all (devices) - I'm at loss here.. 不同的流,不同的设备以及所有(流)可以连接到所有(设备)-我在这里感到迷茫。

The quote does not say that you can connect any stream to any device, as you are saying. 引用并没有说您可以将任何流连接到任何设备。 There are different implementations of InputStream and OutputStream that connect to specific devices - for example, FileInputStream connects to a file on the filesystem, and ByteArrayInputStream connects to a byte array in memory. 连接到特定设备的InputStreamOutputStream有不同的实现-例如, FileInputStream连接到文件系统上的文件,而ByteArrayInputStream连接到内存中的字节数组。

The main idea that the quote is explaining is that all those different kinds of streams are all extensions of the classes InputStream and OutputStream , so that you can do all the common operations on streams using any of the specific kinds of streams, regardless of where the specific kind of stream reads or writes data from or to. 引言所解释的主要思想是,所有这些不同类型的流都是InputStreamOutputStream类的所有扩展,因此您可以使用任何特定类型的流OutputStream执行所有通用操作,无论特定类型的流从中读取数据或向中写入数据。

Some streams are wrappers around other streams, adding specific functionality. 一些流是其他流的包装,从而增加了特定功能。 For example, BufferedOutputStream adds buffering to an underlying stream. 例如, BufferedOutputStream将缓冲添加到基础流。 This is often useful because for some streams, writing in blocks is more efficient than writing byte by byte - BufferedOutputStream collects bytes that you write into a buffer, which is then written to the underlying stream as one block. 这通常很有用,因为对于某些流而言,写块要比逐字节写效率更高BufferedOutputStream收集您写入缓冲区的字节,然后将其作为一个块写入基础流。 ObjectOutputStream is another wrapper, which adds the functionality to convert serializable Java objects to bytes which can be written to an underlying stream. ObjectOutputStream是另一个包装器,它添加了将可序列化的Java对象转换为可以写入基础流的字节的功能。

You cannot use every Stream for every device. 您不能为每个设备使用每个Stream。 According to the definition in your question (bold by me), 根据您问题中的定义(我大胆),

All streams behave in the same manner. 所有流的行为均相同。

So you can use every Stream the same, which means every Stream has the same methods since they inherit from java.io.OutputStream or java.io.InputStream . 因此,可以对每个Stream使用相同的方法,这意味着每个Stream都具有相同的方法,因为它们继承自java.io.OutputStreamjava.io.InputStream

So it does not matter whether you want to write to the console or a file or a networt socket, you can eg always write a byte array to the device. 因此,无论您要写入控制台还是文件或网络套接字,都没有关系,例如,您可以始终将字节数组写入设备。

Nonetheless, there are different implementations which handle writing this byte array differently. 但是,有不同的实现方式可以不同地处理此字节数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM