Java GBK,UTF-8 Encoding

Source: Internet
Author: User

Ching

Original address: http://blog.csdn.net/qingdujun/article/details/41366301


1, Gbk,utf-8 Code

Note: The general default 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{//default is GBK encoding outputstreamwriter OSW = new OutputStreamWriter (new FileOutputStream ("Gbk.txt")),//outputstreamwriter OSW = new OutputStreamWriter (New FileOutputStream ("Utf_8.txt", " GBK ")///Note that the Gbk.txt file size generated at this time is 4 bytes osw.write (" Dolphin "); Osw.close (); public static void Writeutf_8 () throws ioexception{//default is GBK encoding outputstreamwriter OSW = new OutputStreamWriter (new FileOutputStream ("Utf_8.txt"), "UTF-8");//Note that the Utf_8.txt file size generated at this time is 6 bytes osw.write ("Dolphin"); Osw.close ();}}

2. Encoding and decoding

What format to encode, in what format to decode; encode once, decode 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";//code://The following, because the default is GBK code 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));//decode: String s2 = new string (BGBK1); String s3 = new String (BGBK2, "GBK");//If decoding with UTF-8, the decoding format error will occur//string s4 = new String (BGBK2, "UTF-8"); SYSTEM.OUT.PRINTLN (S2); System.out.println (S3);//This will print "????" System.out.println (S4);}}

3, Gbk,utf-8 all support Chinese code

Interested can go to try: In Notepad write "Unicom" two words, save and then open what will appear, why?


Original address: http://blog.csdn.net/qingdujun/article/details/41366301

References: Java video Bi Xiangdong presenter


Java GBK,UTF-8 Encoding

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.