码迷,mamicode.com
首页 > 编程语言 > 详细

Java 字符串编码 (保存成txt测试)

时间:2015-12-08 08:40:16      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

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

 

Java 字符串编码 (保存成txt测试)

标签:

原文地址:http://www.cnblogs.com/codeskilla/p/5028072.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!