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