Java GBK,UTF-8 Encoding

Source: Internet
Author: User

1, Gbk,utf-8 Code

Note: The general default is GBK encoding.

[Java]View Plaincopyprint?
  1. Package io.dol.sn;
  2. Import Java.io.FileOutputStream;
  3. Import java.io.IOException;
  4. Import Java.io.OutputStreamWriter;
  5. Public class Encodestream {
  6. public static void Main (string[] args) throws IOException {
  7. //WRITEGBK ();
  8. Writeutf_8 ();
  9. }
  10. public static void Writegbk () throws IOException
  11. {
  12. //default is GBK encoding
  13. OutputStreamWriter OSW = new OutputStreamWriter (new FileOutputStream ("Gbk.txt"));
  14. //outputstreamwriter OSW = new OutputStreamWriter (New FileOutputStream ("Utf_8.txt", "GBK"));
  15. //Note that the Gbk.txt file size generated at this time is 4 bytes
  16. Osw.write ("Dolphin");
  17. Osw.close ();
  18. }
  19. public static void Writeutf_8 () throws IOException
  20. {
  21. //default is GBK encoding
  22. OutputStreamWriter OSW = new OutputStreamWriter (new FileOutputStream ("Utf_8.txt"),"UTF-8");
  23. //Note that the Utf_8.txt file size generated at this time is 6 bytes
  24. Osw.write ("Dolphin");
  25. Osw.close ();
  26. }
  27. }

2. Encoding and decoding

What format to encode, in what format to decode; encode once, decode once.

[Java]View Plaincopyprint?
  1. Package io.dol.sn;
  2. Import java.io.IOException;
  3. Import Java.util.Arrays;
  4. Public class Encodestream {
  5. public static void Main (string[] args) throws IOException {
  6. String S1 = "Dolphin";
  7. //code:
  8. //as follows, because the default is GBK encoding
  9. byte[] BGbk1 = S1.getbytes ();
  10. byte[] bGbk2 = s1.getbytes ("GBK");
  11. //arrays.tostring () converts a character array to a string
  12. System.out.println (arrays.tostring (BGBK1));
  13. System.out.println (arrays.tostring (BGBK2));
  14. //decode:
  15. String s2 = new String (BGBK1);
  16. String s3 = New String (BGBK2,"GBK");
  17. //If decoding with UTF-8, there will be a decoding format error
  18. //string s4 = new String (BGBK2, "UTF-8");
  19. SYSTEM.OUT.PRINTLN (S2);
  20. System.out.println (S3);
  21. //This will print out "????"
  22. //system.out.println (S4);
  23. }
  24. }

3, Gbk,utf-8 all support Chinese code

Java GBK,UTF-8 Encoding

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.