Underlying input stream returned zero bytes:java
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.}
網上一種解決方法:oop
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.}