Java File Encoding Example

Source: Internet
Author: User

  1. Converts a string to a byte array, using the default encoding of the current project:
    String s = "Hello abc";
    Byte[] B1 = S.getbytes ();


  2. To convert a string in the "GBK" format:
    byte[] B2 = s.getbytes ("GBK");


  3. To convert a string in the "Utf-8" format:
    byte[] B3 = s.getbytes ("Utf-8");


  4. To convert a string in "utf-16be" format, UTF-16BE is the encoded format of Java:
    byte[] B4 = s.getbytes ("Utf-16be");


  5. When a sequence of bytes (a byte array) is a certain encoding, it is also encoded when it is converted to a string, otherwise garbled.


  6. Use the default encoding transformation for the current project:
    String str1 = new string (b4);


  7. Transform using the specified encoding:
    String str2 = new String (B4, "utf-16be");


  8. Convert bytes to int, shown in 16 binary, repeat 0 and F before last year:
    System.out.print (integer.tohexstring (b & 0xff) + "");


  9. Bytes encoded in "GBK": A Chinese character occupies two bytes, and an English letter takes up one byte.

  10. Bytes encoded in "Utf-8": A Chinese character occupies three bytes, and an English letter takes up one byte.

  11. Bytes encoded in "UTF-16BE": one Chinese character accounts for two bytes and one English letter is two bytes.


  12. The file encoding for Java is: byte byte byte

  13. Text files (. txt) created in the Chinese version of the Windows system are saved in ANSI encoding and can be selected with the Save As action

This article is from the "Personal notes" blog, so be sure to keep this source http://mobileself.blog.51cto.com/4350932/1738721

Java File Encoding Example

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.