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