String.getbytes () and String.tochararray (), the difference between byte arrays and character arrays

Source: Internet
Author: User

String.getbytes () is the conversion of a string into a byte array. String.tochararray () converts a string into an array of characters.

For example

byte bys[] = "National Day 60 Anniversary". GetBytes ();

Char chs[]= "National Day 60 Anniversary". ToCharArray ();

"So what's the difference between a byte array and a character array?" 】

The Read and write methods of the byte input stream and output stream use byte arrays to read and write data, that is, to process the data in bytes as basic clerk. Therefore, the byte stream does not work well with Unicode characters, for example, a Chinese character occupies 2 bytes in a file, and if a byte stream is used, improper reading can result in a "garbled" phenomenon. BYTE classes FileInputStream and FileOutputStream are classes under IO, while the character output stream FileReader and the character input stream filewriter are both reader and writer subclasses.

When you create an output stream that points to a file using the construct method, if the parameter append evaluates to True, the output stream does not flush the file pointed to (if the file already exists), the Wirite method of the output stream starts writing data to the file from the end of the file, the parameter append evaluates to False, The output stream refreshes the file pointed to (if the file already exists). Append with refresh function;

For writer streams, the Write method writes the data to the buffer first, and whenever the buffer overflows, the contents of the buffer are automatically written to the destination, and if the stream is closed, the contents of the buffer are immediately written to the destination. A stream call Flush () (not flash) can flush the current buffer immediately, writing the contents of the current buffer to the destination.

String.getbytes () and String.tochararray (), the difference between byte arrays and character arrays

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.