Understand the difference and connection between Java byte stream and character stream _07

What is "stream" in Java?

    A Java I/O object is called a data stream. Objects that read data into memory are called input streams, and objects that write data from memory are called output streams.

According to the different angles it faces, the flow can be divided into the following types:

According to the direction of data flow, it is divided into input flow and output flow. This classification is not absolute, for example, when writing data to a file, it is the output stream; when reading data, it is the input stream.

According to the unit of processing data, it is divided into byte stream and character stream. Indicates that information is read from or written to the stream in bytes, usually used to read binary data. Character stream: Read from or write information to the stream in units of Unicode characters.

According to the different functions, it is divided into node flow and processing flow. Node streams read and write data from specific data nodes (files, databases, memory, etc.); processing streams are connected to existing streams to provide programs with more functions through data processing.


Dividing streams into byte streams and character streams, the hierarchical structure of streams in Java can be represented by the following diagram: blue is abstract class, green is concrete class.


The byte stream itself does not use the buffer (memory) when operating, and is directly operated on the file itself; while the character stream uses the buffer when operating, and then operates the file through the buffer, as shown in the figure.


This can be better understood by following the output code for byte stream and character stream output.

import java.io.File;
import java.io.OutputStream;
import java.io.FileOutputStream;
public class TestOutputStream
{
	public static void main(String[] args) throws Exception //The exception is thrown directly without processing
	{
		File f = new File("d:" + File.separator + "test.txt"); //File.separaor is the file path separator
		OutputStream out = new FileOutputStream(f);//Instantiate the parent class object through the subclass, OutputStream is an abstract class
		String str = "Hello World!!!";//Prepare a string
		byte b[] = str.getBytes();//Convert string to byte array
		out.write(b);//Output the contents of the byte array	
		//The output stream is not closed with out.clase() at this time
	}
}

You can see the test.txt file in the root directory of the d drive, open it and discover

Even if the byte stream operation is not closed at this time, the output content still exists in the file, which proves that the byte stream directly manipulates the file itself. The following continues to use the character stream to complete, and then observe the effect. (delete the test.txt file first)

import java.io.File;
import java.io.Writer;
import java.io.FileWriter;
public class TestWriter
{
	public static void main(String[] args) throws Exception //The exception is thrown directly without processing
	{
		File f = new File("d:" + File.separator + "test.txt"); //File.separaor is the file path separator
		Writer out = new FileWriter(f);//Instantiate the parent class object through the subclass, Writer is an abstract class
		String str = "Hello World!!!";//Prepare a string
		out.write(str);
		//At this time, the output stream is still not closed with out.clase()
	}
}

You can still see the test.txt file in the root directory of the d drive, but when you open it, you find that it is empty and there is no "Hello World"; 

This is because a buffer is used for character stream operations, and when the character stream is closed, the content in the buffer is forced to be output, but if the program is not closed, the content in the buffer cannot be output. So the conclusion is that the character stream uses the buffer, and the byte stream does not use the buffer.


How to understand the word buffer?

A buffer can be simply understood as a memory area. A buffer can be simply understood as a special piece of memory.
In some cases, if a program frequently operates a resource (such as a file or database), the performance will be very low. At this time, in order to improve performance, a part of the data can be temporarily read into an area of ​​memory, and then directly It is enough to read data from this area, because the reading speed of memory will be faster, which can improve the performance of the program.
In the operation of the character stream, all characters are formed in the memory, and all the content will be temporarily stored in the memory before output, so the buffer is used to temporarily store the data.
If you want to output all the contents of the character stream without closing it, you can use the flush( ) method in the Writer class to complete it. Such as the following code:

import java.io.File;
import java.io.Writer;
import java.io.FileWriter;
public class TestWriterFlush
{
	public static void main(String[] args) throws Exception //The exception is thrown directly without processing
	{
		File f = new File("d:" + File.separator + "test.txt"); //File.separaor is the file path separator
		Writer out = new FileWriter(f);//Instantiate the parent class object through the subclass, Writer is an abstract class
		String str = "Hello World";//Prepare a string
		out.write(str);//Output the content
		out.flush();//Forcibly empty the contents of the buffer, all the contents of the buffer.
		// did not close the output stream with out.clase()
	}
}

The results are as follows:

At this point, the content already exists in the file, which further proves that the content is saved in the buffer.


How to choose byte stream or character stream in development?

All files are stored in bytes on the hard disk or during transmission, including pictures, etc. are stored in bytes, and characters are only formed in memory, so in development, bytes Streams are widely used.

The main difference between byte streams and character streams is how they are handled. The byte stream is the most basic and is mainly used to process binary data, which is processed by bytes. But in practice, a lot of data is text, and the concept of character stream is proposed, which is processed according to the encode of the virtual machine, that is, character set conversion. The two are associated with InputStreamReader, OutputStreamWriter, and are actually associated with byte[ ] and String.

The problem of Chinese characters in actual development is actually caused by the inconsistency of conversion between the character stream and the byte stream. When converting from a byte stream to a character stream, it is actually when byte[ ] is converted to String, public String(byte bytes[ ], String charsetName), there is a key parameter character set encoding, which we usually omit, then The system uses the lang of the operating system, and when the character stream is converted into a byte stream, in fact, when String is converted into byte[], byte[ ] String.getBytes(String charsetName) is the same.

As for java.io, there are many other streams, mainly to improve performance and ease of use, such as BufferedInputStream, PipedInputStream and so on.


Guess you like

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