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)); //这个为什么会是乱码??
}
}
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)); //这个为什么会是乱码??
}
}
解决方案 »
- 完成通用的微博开发接口,只要采用 OAuth v1.0 认证的都支持
- 返回JComboBox的选择那一项
- 关于WindowsBuilder(包括swt&swingDesigner)的问题?
- 打算找份工作,请过来人提提意见啊!!
- 请教JdbTable中根据内容显示不同颜色的问题
- Swing中有没有类似richTextBox的东西?
- 求救怎样通过Java对FTP虚拟目录操作,怎样获取linux上的各分区磁盘剩余空间?
- 请教各位高人如何在Java应用程序动态的添加一个按钮?
- SQLServerJDBC配置,帮帮忙!!
- 接下来该看什么书!!!
- 在命令行下面运行*.java 老是跑出Jbuilder怎么办?
- 如何在Eclipse下面新建一个struts的工程
其中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 在转化为字节流的时候,并不认识汉字,而是直接截取,所以再读的时候,因为信息被截断了很多,就算再怎么编码一致,也不可能正常输出原数据了。