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)