Java file stream operation

1、 Concept

In Java, The input and output of the file is through the stream (stream). A stream must be an active end and a destination end. They can be some areas of computer memory, disk files, or even a URL on the Internet. For a stream, we don't care how data is transmitted. We just need to input data to the source end and obtain data from the destination end.

Stream can be divided into byte stream and character stream according to the unit of processing data. The processing unit of byte stream is byte, which is usually used to process binary files, such as music, picture files, etc. The processing unit of the character stream is characters. Because Java adopts Unicode coding, the Java character stream processes Unicode characters, so the character stream has advantages in operating Chinese characters and internationalization.

2、 Byte stream

All byte stream classes inherit from the two abstract classes of InputStream and OutputStream. The following five input byte stream classes are listed. There is a corresponding relationship between the output byte stream class and the input byte stream class, which will not be listed one by one.

3、 Character stream

All character stream classes inherit from the two abstract classes reader and writer, where reader is the abstract class for reading character stream and writer is the abstract class for writing character stream.

The main problem to be solved by reader and writer is internationalization. The original I / O class library only supports 8-bit byte stream, so it can not handle 16 bit Unicode characters well. Unicode is an international character set, so after adding readers and writers, you can automatically convert between the local character set and the Unicode international character set.

4、 Buffer stream

The byte stream and character stream described above are unbuffered input and output streams, which means that each read and write operation will be handed over to the operating system for processing. Such a practice may have a great impact on the performance of the system, because each operation may cause read, write or network access of disk hardware. Therefore, byte stream and character stream are generally not used directly.

Cache stream is a decorator class, which aims to add buffering functions to the original byte stream and character stream. Taking the character buffer stream as an example, the character buffer stream reads and writes characters from the character stream without requiring the system to process immediately, but buffers some characters, so as to realize efficient reading or writing according to the specified number of characters and lines.

Byte buffer stream:

Character buffer stream:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>