public class StringIOLence {
  public static void main(String[] args) throws IOException{
   String str= "我爱你!";
   StringReader s = new StringReader(str);
   int i;
   while ((i = s.read()) != -1){
     System.out.print((char)i);   //这个正常显示
   }
   System.out.println();
      StringBufferInputStream st = new StringBufferInputStream(str);
   byte[] j = new byte[8];
         st.read(j);
         System.out.println(new String(j));  //这个为什么会是乱码??
  }
}

解决方案 »

  1.   

    因为StringBufferInputStream已经过时了,在文档中很明确说明了它存在编码问题的你应该使用ByteArrayInputStream或者StringReader来进行读取
      

  2.   

    仔细看了jdk,发现原因了。
    其中StringBufferInputStream 的available() 方法解释如下:
        Deprecated. Returns the number of bytes that can be read from the input stream without blocking.我在程序中
    String str="我爱你!";
    StringBufferInputStream st = new StringBufferInputStream(str);
    System.out.println(st.available()); //结果却是4字节。要保持信息必须用8个字节
    byte[] j = new byte[8];
    st.read(j);
    System.out.println(new String(j)); 呵呵,看来StringBufferInputStream 在转化为字节流的时候,并不认识汉字,而是直接截取,所以再读的时候,因为信息被截断了很多,就算再怎么编码一致,也不可能正常输出原数据了。