java IO stream analysis

Let's first look at the concept of flow:

All data in the program is transmitted or saved in the form of streams. When the program needs data, it needs to use the input stream to read the data, and when the program needs to save some data, it needs to use the output stream to complete.

The input and output of the program are saved in the form of streams, and all the files saved in the stream are actually byte files .

 

 

byte stream and character stream

There are two main types of file content manipulation in the java.io package: byte stream and character stream, both of which are divided into input and output operations. The output data in the byte stream is mainly completed by the OutputStream, the input is the InputStream, the output in the character stream is mainly completed by the Writer class, and the input stream is mainly completed by the Reader class. (these four are abstract classes)

Java provides a package Java.io dedicated to input and output functions, including:
     InputStream, OutputStream, Reader, Writer
     InputStream and OutputStream, two are designed for byte streams, mainly used to process bytes or binary objects,
     Reader and Writer. Both are designed for character streams (one character occupies two bytes), and are mainly used to process characters or strings.


The character stream processing unit is 2-byte Unicode characters, which operate on characters, character arrays or strings, respectively, while the byte stream processing unit is 1 byte, operating on bytes and byte arrays. Therefore, the character stream is formed by the Java virtual machine converting bytes into 2-byte Unicode characters, so it has better support for multiple languages! If it is an audio file, picture or song, it is better to use byte stream. If it is related to Chinese (text), it is better to use character stream.
     All files are stored in bytes, on disk. It is not the characters of the file that are preserved, but the characters are first encoded into bytes, and then the bytes are stored to disk. When reading a file (especially a text file), it is also read byte by byte to form a sequence of bytes.

     1. Byte streams can be used for any type of object, including binary objects, while character streams can only handle characters or strings;

     2. The byte stream provides the function of processing any type of IO operation, but it cannot directly handle Unicode characters, while the character stream can be used. The
       byte stream is the most basic, all the subclasses of InputStrem and OutputStream are, mainly used In processing binary data, it is processed by bytes, but in practice, many data are text, and the concept of character stream is proposed, which is processed according to the encode of the virtual machine, that is, the conversion of the character set. The relationship between them is through InputStreamReader, OutputStreamWriter, in fact, through byte[] and String to associate Chinese characters in actual development. The problem of Chinese characters in actual development is actually caused by the inconsistency of conversion between character stream and byte stream.

=================We can also see: ===========
The read() method of the Reader class returns an int: as an integer Characters read (16 bits in two bytes), the range is between 0 and 65535 (0x00-0xffff), if the end of the stream has been reached, return -1


Although read() of inputStream also returns int, because this class is byte stream-oriented, and a byte occupies 8 bits, it returns an int byte value in the range of 0 to 255. Returns the value -1 if no bytes are available because the end of the stream has been reached. Therefore, for values ​​that cannot be represented by 0-255, they must be read with a character stream! For example, Chinese characters.


Operating procedures

There are also corresponding steps in IO operation in Java. Taking file operation as an example, the main operation flow is as follows:

1 Open a file using the File class

2 Specify the location of the output through a subclass of byte stream or character stream

3 Perform read/write operations

4 Turn off input/output

IO operations belong to resource operations, be sure to remember to close


byte stream

The byte stream mainly operates on byte type data, which is subject to the byte array. The main operation classes are OutputStream and InputStream.
 

Byte output stream: OutputStream

OutputStream is the largest parent class of byte output streams in the entire IO package. The definition of this class is as follows:

public abstract class OutputStream extends Object implements Closeable,Flushable

From the above definition, it can be found that this class is an abstract class. If you want to use this class, you must first instantiate the object through a subclass. If you want to operate a file now, you can use the FileOutputStream class. After upcasting, you can instantiate OutputStream

Closeable represents an operation that can be closed, because the program must be closed at the end

Flushable: Represents refresh, clearing the data in memory

The constructor of the FileOutputStream class is as follows:

public FileOutputStream(File file)throws FileNotFoundException


The difference between byte stream and character stream


The use of byte streams and character streams is very similar, so what are the differences other than the operation code?

The byte stream itself does not use the buffer (memory) when operating, it is directly operated with the file itself, while the character stream uses the buffer when operating

When the byte stream operates on the file, even if the resource (close method) is not closed, the file can be output, but if the character stream does not use the close method, nothing will be output, indicating that the character stream uses a buffer and can be used. The flush method forces the buffer to be flushed, so that the content can be output without close


Is it better to use byte stream or character stream in development?

When saving files or transmitting on all hard disks, they are performed in bytes, including pictures, and characters are only formed in memory, so the operation using bytes is the most. of.


If you want a java program to implement a copy function, you should use a byte stream to operate (maybe copy pictures), and use a way of reading and writing (saving memory).

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326946589&siteId=291194637