import java.io.IOException;
import java.io.InputStreamReader;public class TestInputStreamReader{
public static void main(String args[])throws IOException{
InputStreamReader isr=new InputStreamReader(System.in,"GBK");
//"GBK"换成其他字符编码就是乱码
int c=0;
while((c=isr.read())!=13){
System.out.print((char)c);
}
}
}
现在好像有点似懂非懂的感觉,谁来说一下原理,让我彻底明白?谢谢
字符流读取,“GBK”才能使汉字读出来,utf-8等不能
字符串也一样。
同样给你一组二进制的0和1,用不同的编码来解释它,得到的结果是不一样的。
new InputStreamReader(System.in,"GBK");
意思就是说,从System.in这个字节流得到字符流的时候,用GBK作为编码方式。
import java.io.InputStreamReader;public class TestInputStreamReader{
public static void main(String args[])throws IOException{
InputStreamReader isr=new InputStreamReader(System.in,"GBK");
//"GBK"换成其他字符编码就是乱码
int c=0;
while((c=isr.read())!=13){
System.out.print((char)c);
}
isr.close() //这一步也很重要
}
}