The use of byte streams and character streams is very similar. Are there any other differences between them besides the operation code?

In fact, the byte stream itself does not use the buffer (memory) during operation, but the file itself is directly operated, whereas the character stream uses the buffer during operation and then manipulates the file through the buffer, as shown in the following figure.The two file-writing operations are compared, but neither byte stream nor character stream is closed after the byte stream and character stream operations are completed. Example: Use byte stream without closing execution

import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; Public class OutputStreamDemo05 {public static void main(String[] args) throws Exception { Separator = new File("d:" + file. separator + "test.txt"); OutputStream out = null; OutputStream out = null; Out = new FileOutputStream(f); // Instantiate by object polymorphism // Step 3: Prepare a String String STR = "Hello World!!" ; Byte b[] = str.getBytes(); Out.write (b); // Step 4: Close the output stream (not closed at this time) // out.close(); }}Copy the code
Copy the code

Program running results:

The byte stream operation is not turned off, but output is still present in the file, proving that the byte stream operates directly on the file itself. Let’s continue with the character stream and see what happens. Example: Use character stream without closing execution

import java.io.File; import java.io.FileWriter; import java.io.Writer; Public class WriterDemo03 {public static void main(String[] args) throws Exception {// Throw an Exception. Separator = new File("d:" + file. separator + "test.txt"); Writerout = null; writerout = null; writerout = null; Out = new FileWriter(f); // Step 3: Prepare a String String STR = "Hello World!!" ; Out.write (STR); // Step 4: Close the output stream (not closed at this time) // out.close(); }}Copy the code

Program running results:

After the program is running will find file without any content, this is because the character stream operation using the buffer, in the closure of the characters flow will be mandatory to the contents of the buffer to the output, but if the program did not shut down, can not output the contents of the buffer, so come to the conclusion: the characters flow using the buffer, and not using byte stream buffer.

Question: what is a buffer?

You come across the term buffer in a lot of places, so what exactly is a buffer? What does it do? Answer: A buffer can simply be thought of as a special area of memory.

In some cases, if a program frequently operate a resource (e.g., file or database), the performance will be very low, at this time in order to improve performance, it can be part of the data into the temporary memory of an area, after can be directly read data from the area, because read memory speed will be faster, so that we can improve program performance.

In a character stream operation, all characters are formed in memory, and all contents are temporarily stored in memory before output, so a buffer is used to hold data temporarily.

If you want to print the entire character stream without closing it, you can use the Flush () method of the Writer class. Example: Mandatory buffer emptying

import java.io.File; import java.io.FileWriter; import java.io.Writer; Public class WriterDemo04 {public static void main(String[] args) throws Exception {// Separator = new File("d:" + file. separator + "test.txt"); Writerout = null; writerout = null; writerout = null; Out = new FileWriter(f); // Step 3: Prepare a String String STR = "Hello World!!" ; Out.write (STR); // Print out.flush(); // Mandatory emptying the buffer // Step 4: Close the output stream (not closed at this time) // out.close(); }}Copy the code

Program running results:

At this point, the content already exists in the file, further proving that the content is stored in the buffer. This point should be paid special attention to in the future development of readers. Question: is it better to use a byte stream or a character stream? Now that you have learned the basic operations of byte stream and character stream, and have a general understanding of the differences in the operation flow, is it better to use byte stream or character stream in development? All files are stored in bytes on hard disk or in transmission. Images are stored in bytes, and characters are formed only in memory. Therefore, byte stream is widely used in development. The main difference between byte streams and character streams is the way they are handled.

  • 1.Java byte stream

    InputStream is the ancestor of all byte input streams, and OutputStream is the ancestor of all byte output streams.

  • 2.Java character streams

Reader to be the ancestor of all read the string input stream, and the writer are the ancestors of all output string InputStream, OutputStream, Reader and writer is an abstract class. So you can’t just say new. The byte stream is the most basic, and all of the InputStream and OutputStream subclasses are primarily used to deal with binary data, which is processed in bytes. But in practice, a lot of data is text, and put forward the concept of character stream, which is processed according to encode of virtual machine, that is, character set transformation. Between the two by InputStreamReader OutputStreamWriter to associate, is in fact by byte [] and a String to associate.

In practice, Chinese character problems are caused by the disunity between character stream and byte stream. Public String(Byte bytes[], String charsetName) has a key character set encoding that we usually omit when converting a byte stream to a character stream, which is actually converted from byte[] to String. The system will use the operating system lang. The same is true for String [] string.getBytes (String charsetName) when a character stream is converted to a byte stream. As for Java. IO also appeared in many other flow, according to the main is to improve performance and easy to use, such as BufferedInputStream PipedInputStream etc.

note

In Java, its kernel uses Unicode, which means it remembers the Unicode internal encoding for each character. Incompatible with ANSI codes. Bytes. According to The UNICODE standard all characters are 2 bytes. Because characters in Java are encoded in Unicode, the “learn Java” string is 10 bytes long in the Java language.