1, Gbk,utf-8 Code
Note: The general default is GBK encoding.
[Java]View Plaincopyprint?
- 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.
[Java]View Plaincopyprint?
- 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:
- //as follows, because the default 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));
- //decode:
- String s2 = new String (BGBK1);
- String s3 = New String (BGBK2,"GBK");
- //If decoding with UTF-8, there will be a decoding format error
- //string s4 = new String (BGBK2, "UTF-8");
- SYSTEM.OUT.PRINTLN (S2);
- System.out.println (S3);
- //This will print out "????"
- //system.out.println (S4);
- }
- }
3, Gbk,utf-8 all support Chinese code
Java GBK,UTF-8 Encoding