JavaSE - Decorative Streams

Disclaimer: The materials used in this column are written by VIP students of Kaige Academy. Students have the right to remain anonymous and have the right to final interpretation of the article. Kaige Academy aims to promote VIP students to learn from each other based on public notes.

Decorative flow:

Introduction: There are two kinds of streams, one is to deal with the transport speed, and the other is to deal with the specific product. Reading one at a time is slower, so you need to read a batch at a time, and this batch should not be too large. Just focus on 8k.
If it reads 100 bytes each time, the speed will be very slow, so you need to read 8k first, put it in memory, cache it first, and then provide it slowly. For it, when he is not enough, I am reading 8k to cache, then we need to establish a cache mechanism in this link, in this case, I will store 8k here every time, and then I will put 8k in the cache for you after use.

image

Example: Use BufferedInputStream to process photos
as shown in the figure below. The first 54 bytes are the header, and every 3 bytes in the back describe a pixel. The description method is BGR. First, convert the image to bmp format, and establish an input and output channel. Then read its head, and then write out the 54 bytes, write an infinite loop, read one byte at a time, if you add BufferedInputStream, the flow speed will be faster

image

image

BufferedOutputStream
cleaning the cache is to clean up the last little bit of data and write it into the file. It is also fine to use close directly.

image

ObjectOutputStream
is as shown below: It can be converted into bytes through ObjectOutputStream, and data can be output to disk through FileOutputStream. During this period, a BufferedOutputStream can also be added. If you want to restore the object, use FileInputStream

image

Example:
If you want some attributes not to be stored on disk, you only need to add transient. If the object needs to be stored on disk, you need to implement the Serializable interface. You can force it to add a version number, change the version number and it will be come up

image

image

image

then output to disk

image

read it up

image

Java.io.IputStream

image

Java.io.OutputStream
ObjectOutputStream

image

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324988577&siteId=291194637