服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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


  这是张孝祥老师的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编码,所以当然就
输出乱

扫描关注微信公众号