The difference between Java byte stream and character stream

Source: Internet
Author: User

The byte stream is very similar to the use of a character stream, but is there any other difference between the two in terms of manipulating the code?

In fact, the byte stream in the operation itself does not use the buffer (memory), the file itself is directly manipulated, and the character stream is used in the operation of the buffer, through the buffer re-operation of the file, 12-6 is Shown.

The following is a comparison of two write files, but the output stream is not closed after the operation of the byte stream and the character Stream.

Example: using a byte stream does not close execution

 packageorg.lxh.demo12.byteiodemo; Importjava.io.File; Importjava.io.FileOutputStream; Importjava.io.OutputStream;  public classOutputStreamDemo05 { public Static voidMain (string[] Args)throwsException {//exception thrown, not handled//1th step: Use the file class to find a documentFile f =NewFile ("d:" + file.separator + "test.txt");//declaring a File object//2nd Step: Instantiate the parent class object from the child classOutputStream out =NULL; //prepare an Output objectout =NewFileOutputStream (f); //instantiation with Object polymorphism//the 3rd step: write operationString str = "Hello world!!!"; //Prepare a string     byteb[] =str.getbytes (); //string to byte arrayOut.write (b); //Output the content//4th step: Turn off the output stream//Out.close (); //not close at this time        }        }   

Program Run Result:

The byte stream operation is not turned off at this time, but the contents of the output still exist in the file, which proves that the byte stream is directly manipulating the file Itself. The following continues to use the character stream to complete, and then observe the Effect.

Example: using a character stream does not close execution

 packageorg.lxh.demo12.chariodemo; Importjava.io.File; Importjava.io.FileWriter; Importjava.io.Writer;  public classWriterDemo03 { public Static voidMain (string[] Args)throwsException {//exception thrown, not handled//1th step: Use the file class to find a documentFile f =NewFile ("d:" + file.separator + "test.txt");//declaring a File object//2nd Step: Instantiate the parent class object from the child classWriter out =NULL; //prepare an Output objectout =NewFileWriter (f); //instantiation with Object polymorphism//the 3rd step: write operationString str = "Hello world!!!"; //Prepare a stringOut.write (str); //Output the content//4th step: Turn off the output stream//Out.close (); //not close at this time    }    }   

Program Run Result:

When the program runs, it finds nothing in the file because the character stream operation uses a buffer, and the contents of the buffer are forced to be output when the character stream is closed, but if the program is not closed, the contents of the buffer cannot be exported, so it concludes that the character flow uses a buffer, The byte stream does not use a buffer.

Question: What is buffer?

In many places the term buffer is encountered, so what exactly is a buffer? What's The effect?

Answer: a buffer can be simply understood as a region of Memory.

You can simply interpret the buffer as a special memory.

In some cases, if a program frequently operates a resource (such as a file or database), the performance is very low, at this point in order to improve performance, you can temporarily read some of the data into the memory of a piece of the area, and then directly from the region to read the data, because the read memory speed will be faster, This can improve the performance of the Program.

In a character stream operation, all characters are formed in memory, and all of the content is temporarily stored in memory before output, so buffer staging data is Used.

You can use the Flush () method in the writer class if you want to output all the contents of a character stream without closing it.

Example: Mandatory emptying buffer

 packageorg.lxh.demo12.chariodemo; Importjava.io.File; Importjava.io.FileWriter; Importjava.io.Writer;  public classWriterDemo04 { public Static voidMain (string[] Args)throwsException {//exception throw not handled//1th step: Use the file class to find a documentFile f =NewFile ("d:" + file.separator + "test.txt");//declaring fileObject//2nd Step: Instantiate the parent class object from the child classWriter out =NULL; //prepare an Output objectout =NewFileWriter (f); //instantiation with Object polymorphism//the 3rd step: write operationString str = "Hello world!!!"; //Prepare a stringOut.write (str); //Output the contentOut.flush (); //forcing the contents of the buffer to be emptied//4th step: Turn off the output stream//Out.close (); //not close at this time    }    }   

Program Run Result:

At this point, the contents of the file already exist, further proving that the content is saved in the Buffer. This is particularly noticeable in the future development of the Reader.

Q: Is it good to use a byte stream or a character flow?

After learning the basic operation of the word stream and the character stream, we have probably understood the various differences of the operation flow, so is it good to use the word stream or the character flow in the development?

Answer: using a byte stream is Better.

Before the answer, first for the reader to explain such a concept, all the files on the hard disk or in the transmission are in bytes, including pictures, etc. are stored in bytes, and characters are only in memory will be formed, so in the development, the use of the word stream is more Extensive.


The main difference between a byte stream and a character stream is the way they handle it.

Stream classification:
byte stream of 1.Java
InputStream is the ancestor of all byte input streams, and OutputStream is the ancestor of all byte output streams.
Character Stream of 2.Java
Reader is the ancestor of all read string input streams, While writer is the ancestor of all output strings.
Inputstream,outputstream,reader,writer are abstract Classes. So you can't just new



Byte stream is the most basic, all InputStream and OutputStream subclasses are, mainly used in processing binary data, it is processed by byte
But in fact, a lot of data is text, and put forward the concept of character stream, it is the virtual machine encode to deal with, that is, the conversion of character set
The two are related by inputstreamreader,outputstreamwriter, in fact through byte[] and String.
The problem of Chinese characters appearing in the actual development is actually caused by the transformation of the character stream and the byte Stream.

When converting from byte to stream, it is actually byte[] converted to string,
public string (byte bytes[], string Charsetname)
There is a key parameter to the character set encoding, which is usually omitted, and the system uses the operating system Lang
When the character flow is converted to a byte stream, it is actually a string conversion to byte[],
byte[] string.getbytes (String Charsetname)
That's the same truth.

As for the java.io, there are many other streams that are mainly designed to improve performance and ease of use,
such as bufferedinputstream,pipedinputstream, etc.

The difference between Java byte stream and character stream

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.