Java GBK, UTF-8 coding, javagbkutf-8 Coding

Source: Internet
Author: User

Java GBK, UTF-8 coding, javagbkutf-8 Coding

Author: Qing Dujun

Address: http://blog.csdn.net/qingdujun/article/details/41366301


1, GBK, UTF-8 Encoding

Note: The default value is GBK encoding.

Package io. dol. sn; import java. io. fileOutputStream; import java. io. IOException; import java. io. outputStreamWriter; public class EnCodeStream {public static void main (String [] args) throws IOException {// WriteGBK (); WriteUTF_8 ();} public static void WriteGBK () throws IOException {// The default value is GBK encoding OutputStreamWriter osw = new OutputStreamWriter (new FileOutputStream ("gbk.txt"); // OutputStreamWriter osw = new OutputStreamWriter (new FileOutputStream ("UTF_8.txt ", "GBK"); // bytes, the size of the gbk.txt file in bytes is 4 bytes osw. write ("Dolphin"); osw. close ();} public static void WriteUTF_8 () throws IOException {// The default is GBK encoding OutputStreamWriter osw = new OutputStreamWriter (new FileOutputStream ("UTF_8.txt"), "UTF-8 "); // bytes, the size of the temporary utf_8.txt file is 6 bytes osw. write ("Dolphin"); osw. close ();}}

2. encoding and decoding

If the encoding format is used, the decoding format is used. If the encoding format is used, the decoding is performed once.

Package io. dol. sn; import java. io. IOException; import java. util. arrays; public class EnCodeStream {public static void main (String [] args) throws IOException {String s1 = "Dolphin"; // encoding: // The same as the following, because the default value is GBK encoding byte [] bGbk1 = s1.getBytes (); byte [] bGbk2 = s1.getBytes ("GBK"); // Arrays. toString () converts a character array to a string System. out. println (Arrays. toString (bGbk1); System. out. println (Arrays. toString (bGbk2); // decoding: String s2 = new String (BGbk1); String s3 = new String (bGbk2, "GBK"); // If decoded with a UTF-8, A decoding format error occurs // String s4 = new String (bGbk2, "UTF-8"); System. out. println (s2); System. out. println (s3); // the output "???" // System. out. println (s4 );}}

3. GBK and UTF-8 support Chinese Encoding

If you are interested, you can try writing the word "Unicom" in your notebook, save it, and try again. Why?


Address: http://blog.csdn.net/qingdujun/article/details/41366301

References: Lecture on Java video Bi Xiangdong


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.