1,如何读取inputstream的内容?
2,读取到的东西不能有乱码
int read(byte[] b) 方法可能中断分开中文字符
3,byte b = new byte[streamlength];
inputs.read(b);
temp=new String(b);
System.out.print(temp);
按长度读取出现一大串不可见字符,整个inputstream不完整
...全文
15615打赏收藏
关于inputstream的问题来者有分!!
1,如何读取inputstream的内容? 2,读取到的东西不能有乱码 int read(byte[] b) 方法可能中断分开中文字符 3,byte b = new byte[streamlength]; inputs.read(b); temp=new String(b); System.out.print(temp); 按长度读取出现一大串不可见字符,整个inputstream不完整
1、解决你乱码的问题
首先你必须知道你读的文件是什么字符集的文件
然后可以:temp = new String(b,"你文件的字符集,如:GB2312")
2、解决没有读全的问题
在你的代码byte b = new byte[streamlength];中,你怎么知道streamlength的?
下面是JDK1.3.1对于inputstream返回值的说明:
Returns:
the total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached.
很显然,你要监测read(byte[] b) 函数的返回值,只有返回-1才表示已经读完了。