<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>读取一次 &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/%E8%AF%BB%E5%8F%96%E4%B8%80%E6%AC%A1/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Tue, 28 Apr 2020 02:09:09 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创] InputStream读取多次导致的Unexpected end of ZLIB input stream问题</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-inputstream%e8%af%bb%e5%8f%96%e5%a4%9a%e6%ac%a1%e5%af%bc%e8%87%b4%e7%9a%84unexpected-end-of-zlib-input-stream%e9%97%ae%e9%a2%98/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-inputstream%e8%af%bb%e5%8f%96%e5%a4%9a%e6%ac%a1%e5%af%bc%e8%87%b4%e7%9a%84unexpected-end-of-zlib-input-stream%e9%97%ae%e9%a2%98/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Tue, 29 Apr 2014 15:17:03 +0000</pubDate>
				<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[InputStream]]></category>
		<category><![CDATA[java.io.EOFException]]></category>
		<category><![CDATA[Unexpected end of ZLIB input stream]]></category>
		<category><![CDATA[读取一次]]></category>
		<category><![CDATA[读取多次]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=8186</guid>

					<description><![CDATA[<p>
有一次，在一个程序中，对jar包里的一个文本文件使用如下方式来读取：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">InputStream&#160;fin&#160;=&#160;getClass().getResourceAsStream(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;/file_to_read.txt&#34;</span>);
</code></pre>
</section>
<p>然后在后面的代码中，对 fin 进行读取：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">BufferedReader&#160;reader&#160;=&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&#160;BufferedReader(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&#160;InputStreamReader(fin));
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">for</span>&#160;(String&#160;line&#160;=&#160;reader.readLine();&#160;line&#160;!=&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">null</span>;&#160;line&#160;=&#160;reader.readLine())&#160;{
&#160;&#160;&#160;&#160;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//<span class="hljs-doctag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">TODO:</span>&#160;your&#160;code&#160;here</span>
}
</code></pre>
</section>
<p>
读一次是没有问题的，但如果对同一个InputStream对象读取两次，就会报类似于下面的错误：</p>
<blockquote>
<div>
		Error: java.io.EOFException: Unexpected end of ZLIB input stream</div>
<div>
		at java.util.zip.ZipFile$1.fill(ZipFile.java:239)</div>
<div>
		at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:141)</div>
<div>
		at java.io.FilterInputStream.read(FilterInputStream.java:116)</div>
<div>
		at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)</div>
<div>
		at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)</div>
<div>
		at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)</div></blockquote>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-inputstream%e8%af%bb%e5%8f%96%e5%a4%9a%e6%ac%a1%e5%af%bc%e8%87%b4%e7%9a%84unexpected-end-of-zlib-input-stream%e9%97%ae%e9%a2%98/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
有一次，在一个程序中，对jar包里的一个文本文件使用如下方式来读取：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">InputStream&nbsp;fin&nbsp;=&nbsp;getClass().getResourceAsStream(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;/file_to_read.txt&quot;</span>);
</code></pre>
</section>
<p>然后在后面的代码中，对 fin 进行读取：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">BufferedReader&nbsp;reader&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;BufferedReader(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;InputStreamReader(fin));
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">for</span>&nbsp;(String&nbsp;line&nbsp;=&nbsp;reader.readLine();&nbsp;line&nbsp;!=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">null</span>;&nbsp;line&nbsp;=&nbsp;reader.readLine())&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//<span class="hljs-doctag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">TODO:</span>&nbsp;your&nbsp;code&nbsp;here</span>
}
</code></pre>
</section>
<p>
读一次是没有问题的，但如果对同一个InputStream对象读取两次，就会报类似于下面的错误：</p>
<blockquote>
<div>
		Error: java.io.EOFException: Unexpected end of ZLIB input stream</div>
<div>
		at java.util.zip.ZipFile$1.fill(ZipFile.java:239)</div>
<div>
		at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:141)</div>
<div>
		at java.io.FilterInputStream.read(FilterInputStream.java:116)</div>
<div>
		at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)</div>
<div>
		at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)</div>
<div>
		at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)</div>
<div>
		at java.io.InputStreamReader.read(InputStreamReader.java:167)</div>
</blockquote>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
其实InputStream只能读取一次，InputStream里的数据就像一个杯子里的水，倒出来（读一遍）之后就没了，不能读两次。</p>
<div>
	如果非要读两次，就用同样的方法再生成一个InputStream对象即可。</p>
<p style="font-size: 16px; margin: 5px 0px; clear: both; font-family: sans-serif;">
		<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
		<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
		转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
		感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
		<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-inputstream%e8%af%bb%e5%8f%96%e5%a4%9a%e6%ac%a1%e5%af%bc%e8%87%b4%e7%9a%84unexpected-end-of-zlib-input-stream%e9%97%ae%e9%a2%98/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
