摘要:拥有包含的字节图像,我怎样才能获得一个干净且正确的阅读器,它返回文本文件的行?a.zip
a.txt
我确实将 zip 文件的图像从 Web 服务下载到byte[] content
. 我想写一个方法
private BufferedReader contentToBufferedReader(byte[] content)
这将返回一个可以像这样使用的阅读器
reader = contentToBufferedReader(content);
while ((line = reader.readLine()) != null) {
processThe(line);
}
reader.close()
到目前为止,我已经(更新)
private BufferedReader contentToBufferedReader(byte[] content) {
ByteArrayInputStream bais = new ByteArrayInputStream(content);
ZipInputStream zipStream = new ZipInputStream(bais);
BufferedReader reader = null;
try {
ZipEntry entry = zipStream.getNextEntry();
// I need only the first (and the only) entry from the zip file.
if (entry != null) {
reader = new BufferedReader(new InputStreamReader(zipStream, "UTF-8"));
System.out.println("contentToBufferedReader(): success");
}
}
catch (IOException e) {
System.out.println("contentToBufferedReader(): failed...");
System.out.println(e.getMessage());
}
return reader;
}
我不确定当出现故障时如何关闭所有流对象。reader
此外,如果成功返回、使用和关闭,我不确定如何关闭它们。