The difference between the Java IO IO class and the connection

Java IO package explanation

The inheritance relationship of the IO class is as follows:

Byte stream:

image-20210315152528221

Character stream:

image-20210315154310097

There is no need to talk about the specific use, and roughly talk about the relationship:



Byte stream and character stream

The byte stream is to read and read the file in the form of bytes.

Suitable for operation 图片, 程序and other non-text files

The character stream is to read and read the file in the form of characters.

Suitable for operation文本文件



Byte stream

InputStreamSum OutputStreamis a base class and cannot be used directly



FileInputStreamReading byte by byte requires a lot of IO operations

BufferedInputStreamIt reads the bytes into the cache first, and then performs a one-time operation. The number of IO operations is small and the efficiency is high.

BufferedInputStreamNeed to FileInputStreambe constructed by

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));



FileOutputStreamAnd the BufferedOutputStreamsame




Character stream

ReaderSum Writeris a base class and cannot be used directly



FileReaderAnd BufferReaderrelationships with FileInputStreamand BufferedInputStreamare similar, and even the structure is the same way

However, InputStreamReaderand BufferedReaderthe relationship is a bit more complicated
because InputStreamReadera byte character bridge, so InputStreamReaderyou need to use FileInputStreamto create, then, can be used to createBufferedReader



OutputStreamWriter, FileWriter, BufferWriterRelationship between the three above

Guess you like

Origin blog.csdn.net/weixin_44062380/article/details/114837526