• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

关于java中字符编码的一点心得,可能对初学者有点帮助

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 21次 | 进入软件测试论坛讨论

领测软件测试网 这是张孝祥老师的java就业培训视频教程里面的一道题目(有所变动):
编写下面的程序代码,分析和观察程序的运行结果:

import java.io.*;
public class TestCodeIO {
      public static void main(String[] args) throws Exception{
            InputStreamReader isr = new InputStreamReader(System.in,"iso8859-1");
            BufferedReader br = new BufferedReader (isr);
            String strLine = br.readLine();
            br.close();
            isr.close();
            System.out.println(strLine);
      } 
}
运行程序后,输入“中国”两个字,输出结果为 ???ú
请按照下面两种方法修改上述程序,是输入的中文能够正常输出
1。修改程序中的语句
              InputStreamReader isr = new InputStreamReader(System.in,"iso8859-1");
2。不修改上面的语句,修改下面的语句
              System.out.println(strLine);


第一种该法很简单,只要改成下面这样就可以了,这里不详细讨论
            InputStreamReader isr = new InputStreamReader(System.in,"gb2312");


这里我要详细讨论的是第二种该法怎么改

起初我是这样改的
          System.out.println(new String (strLine.getBytes(),"iso8859-1"));
输入“中国”后输出的结果虽然不是上面所述的乱码,但是还是乱码,显然这种该法是不正确的!

这里我要感谢 软件民工  告诉我的正确改法,使我恍然大悟
           System.out.println(new String (strLine.getBytes("iso8859-1")));

这两种改法究竟有什么区别呢?为了方便大家阅读,我先把正确和错误的改法帖出来:
import java.io.*;
     public class TestCodeIO {
           public static void main(String[] args) throws Exception{
                 InputStreamReader isr = new InputStreamReader(System.in,"iso8859-1");
                       //Create an InputStreamReader that uses the given charset decoder
                 BufferedReader br = new BufferedReader (isr);
                 String strLine = br.readLine();
                 br.close();
                 isr.close();
                 System.out.println(strLine);
                 System.out.println(new String (strLine.getBytes(),"iso8859-1"));//错误改法
                       //Encodes this String (strLine) into a sequence of bytes using the platform’s 
                       //default charset(gb2312) then constructs a new String by decoding the 
                      //specified array of bytes using the specified charset (iso8859-1)
                      //because this String (strLine) uses the charset decoder "iso8859-1",so it can
                      //only be encoded by "iso8859-1",cann’t be encoded by the platform’s default
                      //charset "gb2312",so this line is wrong.
                System.out.println(new String (strLine.getBytes("iso8859-1")));//正确改法
                     //Encodes this String (strLine) into a sequence of bytes using the named 
                     //charset (iso8859-1),then constructs a new String by decoding the 
                     //specified array of bytes using the platform’s default charset (gb2312).
                     //This line is right.
       } 
}

上面的英文注释已经说得很清楚了,这里我还是解释一下吧:

首先是错误的改法  System.out.println(new String (strLine.getBytes(),"iso8859-1"));
这句代码是将strLine中的字符串用系统默认的编码方式(这里是gb2312)
转换为字节序列,然后用指定的编码方式(这里是iso8859-1)构造一个新的
String对象,并打印到屏幕上。
错误在哪里呢? 
请注意这一段代码  
InputStreamReader isr = new InputStreamReader(System.in,"iso8859-1");
BufferedReader br = new BufferedReader (isr);
String strLine = br.readLine();
这里strLine存储的内容是用指定的编码方式(iso8859-1)存储的,而转换成字节码
的时候(这句代码strLine.getBytes())却使用了系统默认的gb2312编码,所以当然就
输出乱码了!然后用gb2312编码的字节序列构建新的String对象的时候又使用了
iso8859-1编码,所以输出的乱码和System.out.println(strLine)有所不同。


至于正确的改法就不用详细说明了吧,首先将strLine用iso8859-1编码方式转换成字节
序列,然后用系统默认的编码方式(gb2312)构建新的String对象,然后打印输出。

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网