The difference between reading and writing JAVA byte stream and character stream degree

Source: Internet
Author: User

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 files by byte stream     Public Static voidbytewrite () {File file=NewFile ("D:\\test\\test.txt"); Try{outputstream OutputStream=Newfileoutputstream (file); String TextString= "Hello World!!!"; byte[] data =textstring.getbytes ();            Outputstream.write (data); //outputstream.close ();}Catch(IOException e) {System.out.println ("The byte stream writes a file error. ");        E.printstacktrace (); }    }

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 files by character stream     Public Static voidstringwrite () {File file=NewFile ("D:\\test\\test.txt"); Try{FileWriter writer=NewFileWriter (file); String TextString= "Hello World!!!";            Writer.write (TextString); //Writer.flush (); //writer.close ();}Catch(IOException e) {System.out.println ("A character stream has failed to write to the file.");        E.printstacktrace (); }    }

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.

Ask: Is it good to use a byte stream or a character flow?
After learning the basic operation of the byte stream and the character stream, has probably understood the operation process of the various differences, then in the development of the use of the word stream good or character flow good?
Answer: Using a byte stream is better.
before the answer, 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 their way of handling
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. 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 it is not directly new 

Byte stream is the most basic, all the InputStream and OutputStream subclasses are, mainly used in processing binary data, it is processed in bytes, but in fact, a lot of data is text, and put forward the concept of character stream, It is handled by the encode of the virtual machine, 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 the conversion between the character stream and the byte stream, resulting in the
when converting from bytes to a stream of characters, is actually byte[] when converted to string, public string (byte bytes[], string CharsetName) has a key parameter character set encoding, which is usually omitted, when the system is using the operating system Lang and when the character flow into a byte stream, in fact, when the string is converted to byte[], byte[]    String.getbytes (String charsetname) is the same thing
as for the java.io also appeared a lot of other flows, according to the main is to improve performance and ease of use, such as Bufferedinputstream, PipedInputStream, etc.

Differences between read and write JAVA byte stream and character stream degrees

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.