黑马程序员技术交流社区

标题: 关于String的一个小程序怎么不能读取中文 怎么修改 [打印本页]

作者: 王晓龙    时间: 2012-8-6 11:01
标题: 关于String的一个小程序怎么不能读取中文 怎么修改
代码:


import java.io.IOException;

public class Exercise5 {

        public static void main(String[] args) throws IOException {
                System.out.println("请输入:");
                System.out.println(readLine());
                System.out.println(readLine());
                System.out.println(readLine());
        }

       
        public static String readLine() throws IOException {
                StringBuilder sb = new StringBuilder();               
                while (true) {                                                               
                        byte b1 = (byte) System.in.read();               
                        if (b1 == '\r')                                                       
                                continue;                                                       
                        else if (b1 == '\n')                                       
                                break;                                                               
                       
                                sb.append((char)b1);                               
                }
                return sb.toString();                                               
        }
}
输出的 截图 为什么打中文 显示????



QQ截图20120806110017.png (1.16 KB, 下载次数: 22)

QQ截图20120806110017.png

作者: 唐志兵    时间: 2012-8-6 11:13
本帖最后由 唐志兵 于 2012-8-6 11:23 编辑

围观。。。。。。。。。。。。。。
  1. import java.io.IOException;


  2. public class Exercise5 {

  3. public static void main(String[] args) throws IOException {
  4. while (true) {
  5. System.out.println("请输入:");
  6. System.out.println(readLine());
  7. }
  8. }

  9. public static String readLine() throws IOException {
  10. byte[] bt = new byte[1024];
  11. //byte b1 = (byte) System.in.read(); //read()返回的是整数,表示接收到的字节数
  12. int len = System.in.read(bt);
  13. String str = new String(bt, 0, len);
  14. return str;
  15. }
  16. }
复制代码

作者: 包晗    时间: 2012-8-6 11:17
中国话  这三个字不识别
需要设定对应的表
GBK  或者 -8

作者: 乐凡    时间: 2012-8-6 11:30
System.in属于标准的字节输入流,而一个汉字是两个字节,属于String类型的 所以无法输入汉字

作者: 官文昌    时间: 2012-8-6 11:50
字符编码不对 ,换成GBK或者UTF-8,TF-8:Unicode Transformation Format-8bit,允许含BOM,但通常不含BOM。是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24为(三个字节)来编码。UTF-8包含全世界所有国家需要用到的字符,是国际编码,通用性强。UTF-8编码的文字可以在各国支持UTF8字符集的浏览器上显示。如,如果是UTF8编码,则在外国人的英文IE上也能显示中文,他们无需下载IE的中文语言支持包。  
GBK是国家标准GB2312基础上扩容后兼容GB2312的标准。GBK的文字编码是用双字节来表示的,即不论中、英文字符均使用双字节来表示,为了区分中文,将其最高位都设定成1。GBK包含全部中文字符,是国家编码,通用性比UTF8差,不过UTF8占用的数据库比GBD大

总结:
在基于Java的编程中,经常会碰到汉字的处里及显示的问题,比如一大堆乱码或问号。
这是因为JAVA中默认的编码方式是UNICODE,而中国人通常使用的文件和DB都是基于GB2312或者BIG5等编码,
1、在网页中输出中文。
JAVA在网络传输中使用的编码是"ISO-8859-1",故在输出时需要进行转化,如:
String str="中文";
str=new String(str.getBytes("GB2312"),"8859_1");
但假如在编译程序时,使用的编码是“GB2312”,且在中文平台上运行此程序,不会出现此问题,一定要注重。
2、从参数中读取中文
这正好与在网页中输出相反如:
str=new String(str.getBytes("8859_1"),"GB2312");
3、操作DB中的中文问题
一个较简单的方法是:在“控制面扳”中,把“区域”设置为“英语(美国)”。假如还会出现乱码,还可进行如下设置:
取中文时:str=new String(str.getBytes("GB2312"));
向DB中输入中文:str=new String(str.getBytes("ISO-8859-1"));
4、在jsp中的中文解决:
在“控制面扳”中,把“区域”设置为“英语(美国)”.
在JSP页面中加入:
假如还不行正常显示,则还要进行下面的转换:
如:name=new String(name.getBytes("ISO-8859-1"),"GBK");
就不会出现中文问题了。
作者: hello world    时间: 2012-8-6 13:39
        public static String readLine() throws IOException {
                StringBuilder sb = new StringBuilder();                // 定义一个StringBuilder用来存储读取到的字符
                while (true) {                                                                // 定义循环
                        byte b1 = (byte) System.in.read();                // 使用System.in.read()读一个字节
                        if (b1 == '\r')                                                        // 如果读到\r
                                continue;                                                        // 不存, 进入下一次循环
                        else if (b1 == '\n')                                        // 如果读到\n
                                break;                                                                // 代表读到了末尾, 循环结束
                        /*if (b1 < 0) {                                                        // 读取到的字节如果小于0, 代表遇到了中文
                                byte b2 = (byte) System.in.read();        // 中文是2个字节, 所以再读一个
                                String s = new String(new byte[] { b1, b2 });         // 将2个字节转为字符串
                                sb.append(s);                                                // StringBuilder存储2个字节转为的字符
                        } else*/
                                sb.append((char)b1);                                // 不小于0的字节, 代表是英文字符, 直接转为char, 存到StringBuilder中
                }
                return sb.toString();                                                // 循环结束后, 将StringBuilder中的内容转为String返回
        }
}
作者: 王晓龙    时间: 2012-8-6 18:37
刘向阳 发表于 2012-8-6 13:39
public static String readLine() throws IOException {
                StringBuilder sb = new StringBuilder();                //  ...

这个 程序可以打汉字 原来中文的都是负的数值啊   谢谢啦




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