Java string encoding (saved as TXT test)

Source: Internet
Author: User

1.

There are no zzTest.txt files at this time

1  Public classTtest2 {3      Public Static voidFilewrite_overwrite (String _strfullfilename)throwsException4     {5File File =NewFile (_strfullfilename);6         if(!file.exists ())7         {8 file.createnewfile ();9System.out.println (file + "created! ");Ten         } One          AString strcontent = "Test"; -  -randomaccessfile mm =NewRandomaccessfile (file, "RW"); theMm.write (Strcontent.getbytes ("Utf-8")); - mm.close (); -     } -      +      Public Static voidMain (string[] args)throwsException -     { +Filewrite_overwrite ("F:\\zc_code_e\\workspace__myeclipse2013\\jpgnamewithchinese\\zztest.txt"); A     } at}

Open zzTest.txt with EditPlus, as shown below:

2.

Delete the above ZzTest.txt and execute the following code:

1  Public classTtest2 {3      Public Static voidFilewrite_overwrite (String _strfullfilename)throwsException4     {5File File =NewFile (_strfullfilename);6         if(!file.exists ())7         {8 file.createnewfile ();9System.out.println (file + "created! ");Ten         } One          AString strcontent = "Test"; -Strcontent =NewString (Strcontent.getbytes (), "utf-8");//more of this sentence -  therandomaccessfile mm =NewRandomaccessfile (file, "RW"); -Mm.write (Strcontent.getbytes ("Utf-8")); - mm.close (); -     } +      -      Public Static voidMain (string[] args)throwsException +     { AFilewrite_overwrite ("F:\\zc_code_e\\workspace__myeclipse2013\\jpgnamewithchinese\\zztest.txt"); at     } -}

Results:

3.

Delete the previous zzTest.txt, and then execute the following code:

1  Public classTtest2 {3      Public Static voidFilewrite_overwrite (String _strfullfilename)throwsException4     {5File File =NewFile (_strfullfilename);6         if(!file.exists ())7         {8 file.createnewfile ();9System.out.println (file + "created! ");Ten         } One          A         //String strcontent = "Test"; -         //strcontent = new String (Strcontent.getbytes (), "Utf-8"); -  the         byte[] BytesUTF8 =New byte[6]; -Bytesutf8[0] = (byte) 0xe6; -BYTESUTF8[1] = (byte) 0xb5; -BYTESUTF8[2] = (byte) 0x8b; +BYTESUTF8[3] = (byte) 0xe8; -BYTESUTF8[4] = (byte) 0xAF; +BYTESUTF8[5] = (byte) 0x95; AString strcontent =NewString (bytesUTF8, "Utf-8"); at  -randomaccessfile mm =NewRandomaccessfile (file, "RW"); -Mm.write (Strcontent.getbytes ("Utf-8")); - mm.close (); -     } -      in      Public Static voidMain (string[] args)throwsException -     { toFilewrite_overwrite ("F:\\zc_code_e\\workspace__myeclipse2013\\jpgnamewithchinese\\zztest.txt"); +     } -}

Result: Same as Figure one

4.

Delete the previous zzTest.txt, and then execute the following code:

 Public classttest{ Public Static voidFilewrite_overwrite (String _strfullfilename)throwsException {File file=NewFile (_strfullfilename); if(!file.exists ())            {File.createnewfile (); System.out.println (File+ "Created! "); }                //String strcontent = "Test"; //strcontent = new String (Strcontent.getbytes (), "Utf-8");        /*byte[] bytesUTF8 = new Byte[6];        Bytesutf8[0] = (byte) 0xe6;        BYTESUTF8[1] = (byte) 0xb5;        BYTESUTF8[2] = (byte) 0x8b;        BYTESUTF8[3] = (byte) 0xe8;        BYTESUTF8[4] = (byte) 0xAF;        BYTESUTF8[5] = (byte) 0x95;        String strcontent = new String (bytesUTF8, "utf-8"); */        byte[] BYTESGBK =New byte[4]; bytesgbk[0] = (byte) 0xb2; bytesgbk[1] = (byte) 0xe2; bytesgbk[2] = (byte) 0xca; bytesgbk[3] = (byte) 0xd4; String strcontent=NewString (BYTESGBK, "GBK"); Randomaccessfile mm=NewRandomaccessfile (file, "RW"); Mm.write (Strcontent.getbytes ("Utf-8"));    Mm.close (); }         Public Static voidMain (string[] args)throwsException {filewrite_overwrite ("F:\\zc_code_e\\workspace__myeclipse2013\\jpgnamewithchinese\\zztest.txt"); }}

Result: Same as Figure one

C

Java string encoding (saved as TXT test)

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.