黑马程序员技术交流社区

标题: 关于字符编码问题!! [打印本页]

作者: 游泳的猫    时间: 2014-11-25 23:39
标题: 关于字符编码问题!!
代码如下:
package studyreflect;

import java.io.UnsupportedEncodingException;

public class CharCode {

        /**
         * @param args
         * @throws UnsupportedEncodingException
         */
        public static void main(String[] args) throws UnsupportedEncodingException {
                System.getProperties().put("file.encoding", "iso8859-1");
                System.getProperties().list(System.out);
                String str="中国";
                byte[] buf=str.getBytes();
                for(int i=0;i<buf.length;i++){
                        System.out.println(Integer.toHexString(buf[i]));
                }
                for(int i=0;i<buf.length;i++){
                        System.out.write(buf[i]);
                }
                System.out.println();

        }
}
问题1.这是《java就业培训》书中的一段代码,书中的最后结果"中国"不能打印出来,可是我的能,不知道为什么?求解答,这道题的答案关系到第二题。。。
问题2.System.out.println("中国")这句代码的具体过程,书中是这样说的:先把“中国”转换其GB2312码的字节数组,然后调用write方法将这字节数组写入到输出流中。这样对吗?
以上两道题急求大神解答,现在这里谢谢了。。。。。








欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2