Basic java record-bytes and characters

Source: Internet
Author: User

All files are stored in bytes. What is stored on the disk is not the character of the file, but the characters are first encoded into bytes, and then stored on the disk. When reading a file (especially a text file), it is also a byte read to form a byte sequence.
Int I = 7 so I is a character. in Java, two bytes are used to indicate that the value is 0x0007.

______________________________________________________________________________________________


1. byte streams can be used for any type of objects, including binary objects. The byte stream can only process characters or strings;

2. A byte stream provides the ability to process any type of IO operations, but it cannot directly process Unicode characters, while a bytes stream can.

______________________________________________________________________________________________
Byte streams are the most basic. All InputStrem and OutputStream subclasses are used to process binary data. They are processed by byte.
However, in reality, a lot of data is text, and the concept of character stream is proposed. It is processed by the encode of the virtual machine, that is, Character Set conversion.
The two are associated through InputStreamReader and OutputStreamWriter. In fact, they are associated through byte [] and String.
In actual development, the Chinese character problems are actually caused by inconsistent conversion between the primary stream and the word throttling.

When the bytes are converted to bytes streams, the bytes [] is actually converted to strings,
Public String (byte bytes [], String charsetName)
For example, // decodes a string by GB2312.
Hello = new String (hello. getBytes (), "GB2312 ");
There is a key parameter character set encoding, which is usually omitted. Then the system uses the lang of the operating system.
When the bytes stream is actually converted into byte,
Byte [] String. getBytes (String charsetName)
For example, the character string is decoded by UTF8 to throttling, and the corresponding bytes are printed.
Hello = new String (hello. getBytes ("UTF8 "));
The same is true.

As for java. io, there are many other streams, mainly to improve performance and ease of use,
Such as BufferedInputStream and PipedInputStream.

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.