IO流:异常Underlying input stream returned zero bytes

Underlying input stream returned zero bytes:

 

1.//方法一 【抛出异常java.io.IOException : underlying input stream returned zero bytes】 
2.String line = null; 
3.while ((line=br.readLine()) != null) { 
4.System.out.println("line = "+line); 
5.} 
6.
7.//方法二 【打印结果为b=0 b=0】 
8.ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
9.int ch; 
10.while((ch=inputStream.read()) != -1){ 
11.bos.write(ch); 
12.} 
13.byte[] bt = bos.toByteArray(); 
14.bos.close(); 
15.for(byte b : bt){ 
16.System.out.print("b="+ b +"\t"); 
17.}

 网上一种解决方法:

 

1.BufferedReader inStream = new BufferedReader(new InputStreamReader(inputStream));  
2.  
3.byte ch = 0;  
4.  
5.char myChar;  
6.  
7.int charVal;  
8.  
9.try{  
10.  
11.while((ch = (byte)inStream.read()) != -1){  
12.  
13.if((ch == 13) || (ch == 10)){ //In ASCII code: 13 = Carriage return, 10 = line feed. When the GPS receiver sends those characters, the while loop must be broken to avoid an IOException  
14.  
15.break;  
16.  
17.}else{  
18.  
19.myChar = (char)ch;  
20.  
21.charVal = myChar;  
22.  
23.//System.out.print("byte=" + ch +" myChar=" + myChar + " charVal=" + charVal + "\n");  
24.  
25.System.out.print(myChar);  
26.  
27.}  
28.  
29.}

 

 

转载于:https://my.oschina.net/u/175660/blog/74394

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值