Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

What is an IO stream

See the explanation of convection of Baidu Encyclopedia first

Flow is an abstract concept that represents the unstructured transfer of data. Input and output are streamed, and data is treated as an unstructured byte order or sequence of characters. The operation of getting data from a stream is called an extract operation, while the operation of adding data to a stream is called an insert operation. The stream used for input and output operations is called an IO stream. In other words, an IO stream is a stream of input and outputCopy the code

IO streams can be explained by the following diagram. In layman’s terms, reading data from disk into memory is called an input stream, while writing data from memory to disk is called an output stream. JAVA IO streams are a means of communication between JAVA programs and the operating system.

Byte stream and character stream

Way, as the name implies, bytes read in a byte read a byte, and the characters flow way of reading is a character read a character, the character of stream processing unit for two bytes of Unicode characters, operation characters, character array or string, respectively, and the byte stream processing unit for 1 byte, operating bytes and an array of bytes. If it is audio file, picture, song, use byte stream, if it is related to Chinese (text), use character stream. All files are stored in bytes. Instead of preserving the characters of files on disk, characters are encoded into bytes and then stored on disk. When reading a file (especially a text file), byte by byte is also read to form a byte sequence. The byte stream can be used for any type of object, including binary objects, whereas the character stream can only handle characters or strings. Byte streams provide functionality for handling any type of IO operations, but they cannot handle Unicode characters directly, as character streams can.

FileInputStream && FileOutputStream

FileInputStream and FileOutputStream handle byte streams, which handle input and output, respectively. Input and output can be byte by byte or multiple bytes at a time.

A constructor

For FileInputStream, the constructor is simple, passing in the path of the file to be read; For FileOutputStream, there are two constructors. The first is to pass in the path of the file to be written, as FileInputStream does, and the other is to pass in a second argument (true or false) to override the file or continue writing after the original file.

public FileInputStream(String name)
public FileOutputStream(String name)
public FileOutputStream(String name, boolean append)
Copy the code

One byte at a time

//FileInputStream reads one byte at a time
	public int read(a)

//FileOutputStream writes functions one byte at a time
	public void write(int b)
Copy the code

Once multibyte

//FileInputStream reads multiple bytes at a time
	public int read(byte[] b)
	public int read(byte[] b, int off, int len)

//FileOutputStream writes multibyte functions at once
	public void write(byte[] b)
	public void write(byte[] b, int off, int len)
Copy the code

Shut down

It is important to call close() to close the stream after FileInputStream and FileOutputStream have finished executing. This is because a stream is a computer resource that wastes resources if it is not closed, and the output stream cannot be written to a file if it is not closed.

FileReader && FileWriter

FileReader and FileWriter handle streams of characters, which handle input and output, respectively, one character at a time or multiple characters at a time.

A constructor

For FileReader, the constructor is simple, passing in the path of the file to be read. For FileWriter, there are two constructors. The first, like FileReader, takes the path of the file to be written to, and the second (true or false) takes the argument to rewrite the file or continue writing after the original file.

public FileReader(String name)
public FileWriter(String name)
public FileWriter(String name, boolean append)
Copy the code

One byte at a time

//FileReader reads one byte at a time
	public int read(a)

//FileWriter writes functions one byte at a time
	public void write(int b)
Copy the code

Once multibyte

//FileReader reads multiple bytes at a time
	public int read(byte[] b)
	public int read(byte[] b, int off, int len)

//FileWriter writes multibyte functions at once
	public void write(byte[] b)
	public void write(byte[] b, int off, int len)
Copy the code

Shut down

It is important to call close() to close the stream after the FileReader and FileWriter have executed, because the stream is a computer resource that wastes resources if it is not closed, and the output stream cannot write to the file if it is not closed.