<p>Java InputStream读取数据问题</p><p>======================================================================</p><p>原理讲解</p><p>1. 关于InputStream.read()</p><p> 在从数据流里读取数据时,为图简单,经常用InputStream.read()方法。这个方法是从流里每次只读取读取一个字节,效率会非常低。 更好的方法是用InputStream.read(byte[] b)或者InputStream.read(byte[] b,int off,int len)方法,一次读取多个字节。</p><p></p><p>2. 关于InputStream类的available()方法</p><p> 要一次读取多个字节时,经常用到InputStream.available()方法,这个方法可以在读写操作前先得知数据流里有多少个字节可以读取。需要注意的是,如果这个方法用在从本</p><p>地文件读取数据时,一般不会遇到问题,但如果是用于网络操作,就经常会遇到一些麻烦。比如,Socket通讯时,对方明明发来了1000个字节,但是自己的程序调用available()方法却只得到900,或者100,甚至是0,感觉有点莫名其妙,怎么也找不到原因。其实,这是因为网络通讯往往是间断性的,一串字节往往分几批进行发送。本地程序调用available()方法有时得到0,这可能是对方还没有响应,也可能是对方已经响应了,但是数据还没有送达本地。对方发送了1000个字节给你,也许分成3批到达,这你就要调用3次available()方法才能将数据总数全部得到。</p><p> 如果这样写代码:</p><pre class="brush:java;toolbar:false">intcount=in.available(); byte[]b=newbyte[count]; in.read(b);</pre><p>在进行网络操作时往往出错,因为你调用available()方法时,对发发送的数据可能还没有到达,你得到的count是0。</p><p> 需要改成这样:</p><pre class="brush:java;toolbar:false"> intcount=0; while(count==0){ //count=in.available(); count=response.getEntity().getContentLength();//(HttpResponseresponse) } byte[]b=newbyte[count]; in.read(b);</pre><p><br/></p><p>3. 关于InputStream.read(byte[] b)和InputStream.read(byte[] b,int off,int len)这两个方法都是用来从流里读取多个字节的,有经验的程序员就会发现,这两个方法经常 读取不到自己想要读取的个数的字节。比如第一个方法,程序员往往希望程序能读取到b.length个字节,而实际情况是,系统往往读取不了这么多。仔细阅读Java的API说明就发现了,这个方法 并不保证能读取这么多个字节,它只能保证最多读取这么多个字节(最少1个)。因此,如果要让程序读取count个字节,最好用以下代码:</p><pre class="brush:java;toolbar:false">byte[]bytes=newbyte[count]; intreadCount=0;//已经成功读取的字节的个数 while(readCount&lt;count){ readCount+=in.read(bytes,readCount,count-readCount); }</pre><p>用这段代码可以保证读取count个字节,除非中途遇到IO异常或者到了数据流的结尾(EOFException).</p><p>==========================================================================================</p><p>代码分享</p><p>下面分享我自己写的测试代码片段,供大家参考:</p><pre class="brush:java;toolbar:false">/** *&lt;b&gt;获取指定的URL返回的数据信息&lt;/b&gt; *@param&lt;fontcolor=&quot;#efac10&quot;&gt;&lt;ahref=&quot;http://www.baidu.com&quot;&gt;_url:指定的URL&lt;/a&gt;&lt;/font&gt; *@return *@throwsClientProtocolException *@throwsIOException */ publicStringgetReponse(String_url)throwsClientProtocolException,IOException { StringreadContent=null; DefaultHttpClienthttpclient=newDefaultHttpClient(); HttpGethttpget=newHttpGet(SinaJsonTest.SinaUrl); System.out.println(&quot;0.SendtheURLtoSinaSever....&quot;); HttpResponseresponse=httpclient.execute(httpget); HttpEntityentity=response.getEntity(); System.out.println(&quot;1.GetResponseStatus:&quot;+response.getStatusLine()); if(entity!=null){ System.out.println(&quot;GetResponseContentEncoding():&quot;+entity.getContentEncoding()); System.out.println(&quot;ContentLength():&quot;+entity.getContentLength()); //getResponse InputStreamin=entity.getContent(); intcount=0; while(count==0){ count=Integer.parseInt(&quot;&quot;+entity.getContentLength());//in.available(); } byte[]bytes=newbyte[count]; intreadCount=0;//已经成功读取的字节的个数 while(readCount&lt;=count){ if(readCount==count)break; readCount+=in.read(bytes,readCount,count-readCount); } //转换成字符串 readContent=newString(bytes,0,readCount,&quot;UTF-8&quot;);//converttostringusingbytes System.out.println(&quot;2.GetResponseContent():\n&quot;+readContent); } returnreadContent; }</pre>
T:0.007378s,M:252.1 KB
返回顶部 留言