Java: What is the input / output (I / O) flow?

In Java, all data is read and written using the stream. Stream is an ordered sequence of data, the data is taken from one place to another.

According to the different flows can be divided into an input (Input) streams and output (Output) two kinds of flow .
 

He / output streams as input?

Java program to complete the input / output through the flow, all of the input / output processing as a stream. Therefore, to understand the I / O system , we must first understand the concept of input / output streams.

Input is the data read from various input devices (including file, a keyboard, etc.) into memory , output by contrast, is to write data to various output devices (such as a file, display, disk, etc.) . For example, a standard keyboard is an input device, and the display is a standard output device, but the file may be used as an input device, but also as an output device .

The data stream is Java objects for I / O operations, which according to different standards can be divided into different categories .

  • According to the direction of flow is divided into the input stream and output stream into two categories.
  • Data stream according to the data unit into different byte stream and character stream .
  • According to the function can be divided into nodes stream and the process stream .


Processing the data stream only in order to perform the data sequence, i.e. a data after processing previous data to be processed. The program data stream is acquired as an input stream, and then output as the output data stream to other devices. Figure 1 shows an input stream mode, output flow pattern is shown in FIG.
 


1 input stream mode

 


FIG 2 output stream mode

1. The input stream

Java classes are related to the stream function encapsulated in java.io package in, and each data stream is an object . All classes are input stream InputStream abstract class (input stream of bytes) and Reader abstract classes (character input stream) subclass . Wherein InputStream class is an abstract class byte input stream, is the parent class for all input stream of bytes , its hierarchy shown in Figure 3.
 


FIG. 3 the InputStream class hierarchy of FIG.


InputStream class in all methods when errors are encountered will lead to an IOException. Following is a common method included in this class.

  • int read():从输入流读入一个 8 字节的数据,将它转换成一个 0~255 的整数,返回一个整数,如果遇到输入流的结尾返回 -1。
  • int read(byte[] b):从输入流读取若干字节的数据保存到参数 b 指定的字芳数组中,返回的字芾数表示读取的字节数,如果遇到输入流的结尾返回 -1。
  • int read(byte[] b,int off,int len):从输入流读取若干字节的数据保存到参数 b 指定的字节数组中,其中 off 是指在数组中开始保存数据位置的起始下标,len 是指读取字节的位数。返回的是实际读取的字节数,如果遇到输入流的结尾则返回 -1。
  • void close():关闭数据流,当完成对数据流的操作之后需要关闭数据流。
  • int available():返回可以从数据源读取的数据流的位数。
  • skip(long n):从输入流跳过参数 n 指定的字节数目。
  • boolean markSupported():判断输入流是否可以重复读取,如果可以就返回 true。
  • void mark(int readLimit):如果输入流可以被重复读取,从流的当前位置开始设置标记,readLimit 指定可以设置标记的字苟数。
  • void reset():使输入流重新定位到刚才被标记的位置,这样可以重新读取标记过的数据。


上述最后 3 个方法一般会结合在一起使用,首先使用 markSupported() 判断,如果可以重复读取,则使用 mark(int readLimit) 方法进行标记,标记完成之后可以使用 read() 方法读取标记范围内的字节数,最后使用 reset() 方法使输入流重新定位到标记的位置,继而完成重复读取操作

Java 中的字符Unicode 编码,双字节的,而 InputerStream 是用来处理单字节的,在处理字符文本时不是很方便。这时可以使用 Java 的文本输入流 Reader 类,该类是字符输入流的抽象类,即所有字输入流的实现都是它的子类

Reader类的具体层次结构如图 4 所示,该类的方法与 InputerSteam 类的方法类似,这里不再介绍。
 


图4 Reader类的层次结构

2.输出流

在 Java 中所有输出流类都是 OutputStream 抽象类(字节输出流)Writer 抽象类(字符输出流)的子类。其中 OutputStream 类是字节输出流的抽象类,是所有字节输出流的父类,其层次结构如图 5 所示。
 


图5 OutputStream类的层次结构图


OutputStream 类是所有字节输出流的超类,用于以二进制的形式将数据写入目标设备,该类是抽象类,不能被实例化。OutputStream 类提供了一系列跟数据输出有关的方法如下所示。

  • int write (b):将指定字节的数据写入到输出流。
  • int write (byte[] b):将指定字节数组的内容写入输出流。
  • int write (byte[] b,int off,int len):将指定字节数组从 off 位置开始的 len 字芳的内容写入输出流。
  • close():关闭数据流,当完成对数据流的操作之后需要关闭数据流。
  • flush():刷新输出流,强行将缓冲区的内容写入输出流。


字符输出流的父类是 Writer,其层次结构如图 6 所示。
 


图6 OutputStream类的层次结构图

Guess you like

Origin blog.csdn.net/weixin_44015669/article/details/92711478