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

  1. Package Org.lxh.demo12.byteiodemo;
  2. Import Java.io.File;
  3. Import Java.io.FileOutputStream;
  4. Import Java.io.OutputStream;
  5. public class OutputStreamDemo05 {
  6. public static void Main (string[] args) throws Exception {//exception thrown, not processed
  7. 1th Step: Use the file class to find a document
  8. File F = new file ("D:" + File.separator + "test.txt"); Declaring a File object
  9. 2nd Step: Instantiate the parent class object from the child class
  10. OutputStream out = null;
  11. Prepare an Output object
  12. out = new FileOutputStream (f);
  13. Instantiation with Object polymorphism
  14. The 3rd step: write operation
  15. String str = "Hello world!!!";
  16. Prepare a String
  17. byte b[] = Str.getbytes ();
  18. String to byte array
  19. Out.write (b);
  20. Output the Content
  21. 4th step: Turn off the output stream
  22. Out.close ();
  23. Not close at this time
  24. }
  25. }


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

  1. Package Org.lxh.demo12.chariodemo;
  2. Import Java.io.File;
  3. Import Java.io.FileWriter;
  4. Import Java.io.Writer;
  5. public class WriterDemo03 {
  6. public static void Main (string[] args) throws Exception {//exception thrown, not processed
  7. 1th Step: Use the file class to find a document
  8. File F = new file ("D:" + File.separator + "test.txt");//Declare file object
  9. 2nd Step: Instantiate the parent class object from the child class
  10. Writer out = null;
  11. Prepare an Output object
  12. out = new FileWriter (f);
  13. Instantiation with Object polymorphism
  14. The 3rd step: write operation
  15. String str = "Hello world!!!";
  16. Prepare a String
  17. Out.write (str);
  18. Output the Content
  19. 4th step: Turn off the output stream
  20. Out.close ();
  21. Not close at this time
  22. }
  23. }


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

  1. Package Org.lxh.demo12.chariodemo;
  2. Import Java.io.File;
  3. Import Java.io.FileWriter;
  4. Import Java.io.Writer;
  5. public class WriterDemo04 {
  6. public static void Main (string[] args) throws Exception {//exception throw not handled
  7. 1th Step: Use the file class to find a document
  8. File F = new file ("D:" + File.separator + "test.txt");//Declare file
  9. Object
  10. 2nd Step: Instantiate the parent class object from the child class
  11. Writer out = null;
  12. Prepare an Output object
  13. out = new FileWriter (f);
  14. Instantiation with Object polymorphism
  15. The 3rd step: write operation
  16. String str = "Hello world!!!";
  17. Prepare a String
  18. Out.write (str);
  19. Output the Content
  20. Out.flush ();
  21. Forcing the contents of the buffer to be emptied
  22. 4th step: Turn off the output stream
  23. Out.close ();
  24. Not close at this time
  25. }
  26. }



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 and so on.

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.