[原创] InputStream读取多次导致的Unexpected end of ZLIB input stream问题
有一次,在一个程序中,对jar包里的一个文本文件使用如下方式来读取:
InputStream fin = getClass().getResourceAsStream("/file_to_read.txt");
然后在后面的代码中,对 fin 进行读取:
BufferedReader reader = new BufferedReader(new InputStreamReader(fin));
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
//TODO: your code here
}
读一次是没有问题的,但如果对同一个InputStream对象读取两次,就会报类似于下面的错误:
… Read MoreError: java.io.EOFException: Unexpected end of ZLIB input streamat java.util.zip.ZipFile$1.fill(ZipFile.java:239)at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:141)at java.io.FilterInputStream.read(FilterInputStream.java:116)at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)