What are the similarities and differences between JAVA byte stream and character stream reading and writing?

Source: Internet
Author: User
Tags flush

Java processing files in the class, stream at the end of the use of byte stream, reader and writer End is a character stream. The difference between the two is read and write, one is read by Byte, and the other is by character.

The bottom of the character stream is the byte stream. and the character stream is mainly read the text file content, you can read a character one character, you can read a line of text file content. and the byte stream reading unit is byte.byte as the computer storage most basic unit, may use the byte stream to read many other formats the file, the slice video and so on. Based on B/S and c/s file transfer can be used in the form of byte stream. Reading and writing a file requires that the content be processed in rows, such as comparing specific characters, and a character stream is typically selected when processing a row of data. Just read and write files, and the contents of the file independent, the general choice of byte stream. (Note: In addition to character and Byte streams, Java has a kind of called object flow.) Be interested to check the API to learn. )

The byte stream is transmitted in bytes, and the character stream is transmitted by character. The most basic is the byte stream, the character stream can be said to be a packet stream to the byte stream. For example, if you know that a character is 8 bytes, then you let the byte stream transmit 8 bytes at a time, that's not the equivalent of a character stream.

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 to manipulate the file,

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

Write file by byte stream public static void Bytewrite () {    File File = new file ("D:\\test\\test.txt");    try {        OutputStream outputstream = new FileOutputStream (file);        String textstring = "Hello world!!!";        byte[] data = Textstring.getbytes ();        Outputstream.write (data);        Outputstream.close ();    } catch (IOException e) {        System.out.println ("byte stream writes a file error. ");        E.printstacktrace ();    }}

Copy Code
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

Write file by character stream public static void Stringwrite () {    File File = new file ("D:\\test\\test.txt");    try {        FileWriter writer = new FileWriter (file);        String textstring = "Hello world!!!";        Writer.write (textstring);        Writer.flush ();        Writer.close ();    } catch (IOException e) {        System.out.println ("The character stream writes a file error.");        E.printstacktrace ();    }}

Copy Code
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.

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: 1.Java byte stream InputStream is the ancestor of all bytes of input stream, and OutputStream is the ancestor of all bytes output stream. The 2.Java character Stream 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 the 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 the character set is 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.
The public String (byte bytes[], string charsetname) has a key parameter character set encoding, usually omitted, when converting from byte to stream, in fact, byte[] is converted to string. Byte[] String.getbytes (String charsetname) is the same as when the system uses the lang of the operating system and when the character stream is converted to byte streams, when it is actually a string conversion to byte[].
As for java.io, there are many other streams that are mainly designed to improve performance and ease of use, such as Bufferedinputstream,pipedinputstream.

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.