1、
此时没有 zzTest.txt 文件
1 public class Ttest 2 { 3 public static void FileWrite_Overwrite(String _strFullFileName) throws Exception 4 { 5 File file = new File(_strFullFileName); 6 if (! file.exists()) 7 { 8 file.createNewFile(); 9 System.out.println(file + "已创建!"); 10 } 11 12 String strContent = "测试"; 13 14 RandomAccessFile mm = new RandomAccessFile(file, "rw"); 15 mm.write(strContent.getBytes("utf-8")); 16 mm.close(); 17 } 18 19 public static void main(String[] args) throws Exception 20 { 21 FileWrite_Overwrite("F:\ZC_Code_E\workspace__MyEclipse2013\JpgNameWithChinese\zzTest.txt"); 22 } 23 }
用 EditPlus 打开 zzTest.txt,显示如下:
2、
将上面的 zzTest.txt 删掉,然后执行下面的代码:
1 public class Ttest 2 { 3 public static void FileWrite_Overwrite(String _strFullFileName) throws Exception 4 { 5 File file = new File(_strFullFileName); 6 if (! file.exists()) 7 { 8 file.createNewFile(); 9 System.out.println(file + "已创建!"); 10 } 11 12 String strContent = "测试"; 13 strContent = new String(strContent.getBytes(), "utf-8"); // 多了这一句 14 15 RandomAccessFile mm = new RandomAccessFile(file, "rw"); 16 mm.write(strContent.getBytes("utf-8")); 17 mm.close(); 18 } 19 20 public static void main(String[] args) throws Exception 21 { 22 FileWrite_Overwrite("F:\ZC_Code_E\workspace__MyEclipse2013\JpgNameWithChinese\zzTest.txt"); 23 } 24 }
结果:
3、
将前面的 zzTest.txt 删掉,然后执行下面的代码:
1 public class Ttest 2 { 3 public static void FileWrite_Overwrite(String _strFullFileName) throws Exception 4 { 5 File file = new File(_strFullFileName); 6 if (! file.exists()) 7 { 8 file.createNewFile(); 9 System.out.println(file + "已创建!"); 10 } 11 12 //String strContent = "测试"; 13 //strContent = new String(strContent.getBytes(), "utf-8"); 14 15 byte[] bytesUTF8 = new byte[6]; 16 bytesUTF8[0] = (byte)0xE6; 17 bytesUTF8[1] = (byte)0xB5; 18 bytesUTF8[2] = (byte)0x8B; 19 bytesUTF8[3] = (byte)0xE8; 20 bytesUTF8[4] = (byte)0xAF; 21 bytesUTF8[5] = (byte)0x95; 22 String strContent = new String(bytesUTF8, "utf-8"); 23 24 RandomAccessFile mm = new RandomAccessFile(file, "rw"); 25 mm.write(strContent.getBytes("utf-8")); 26 mm.close(); 27 } 28 29 public static void main(String[] args) throws Exception 30 { 31 FileWrite_Overwrite("F:\ZC_Code_E\workspace__MyEclipse2013\JpgNameWithChinese\zzTest.txt"); 32 } 33 }
结果:和 图一 一样
4、
将前面的 zzTest.txt 删掉,然后执行下面的代码:
public class Ttest { public static void FileWrite_Overwrite(String _strFullFileName) throws Exception { File file = new File(_strFullFileName); if (! file.exists()) { file.createNewFile(); System.out.println(file + "已创建!"); } //String strContent = "测试"; //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 = new String(bytesGBK, "GBK"); RandomAccessFile mm = new RandomAccessFile(file, "rw"); mm.write(strContent.getBytes("utf-8")); mm.close(); } public static void main(String[] args) throws Exception { FileWrite_Overwrite("F:\ZC_Code_E\workspace__MyEclipse2013\JpgNameWithChinese\zzTest.txt"); } }
结果:和 图一 一样
C