获取java.io.IOException:流关闭错误,但未显式关闭它
我的zipInputStream在写入第一个文件本身后将关闭,即使我没有关闭任何流获取java.io.IOException:流关闭错误,但未显式关闭它,java,fileoutputstream,zipinputstream,Java,Fileoutputstream,Zipinputstream,我的zipInputStream在写入第一个文件本身后将关闭,即使我没有关闭任何流 ZipInputStream zipInputStream = new ZipInputStream(inputStream); ZipEntry zipEntry = zipInputStream.getNextEntry(); while (zipEntry != null) { modelFolderName = <somefoldername> mo
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
ZipEntry zipEntry = zipInputStream.getNextEntry();
while (zipEntry != null) {
modelFolderName = <somefoldername>
modelFileName = <somefilename>
String FILE_STORAGE_LOCATION = env.getProperty("workspacePath");
File folder = new File(FILE_STORAGE_LOCATION + "/" + modelFolderName );
if(!folder.exists()) {
folder.mkdirs();
}
try (FileOutputStream fout=new FileOutputStream(FILE_STORAGE_LOCATION + "/" + modelFolderName + "/" + modelFileName)) {
try (BufferedInputStream in = new BufferedInputStream(zipInputStream)) {
byte[] buffer = new byte[8096];
while (true) {
int count = in.read(buffer);
if (count == -1) {
break;
}
fout.write(buffer, 0, count);
}
}
}
zipEntry = zipInputStream.getNextEntry();
}
ZipInputStream ZipInputStream=新的ZipInputStream(inputStream);
ZipEntry-ZipEntry=zipInputStream.getNextEntry();
while(zipEntry!=null){
modelFolderName=
模型文件名=
字符串文件_STORAGE_LOCATION=env.getProperty(“workspacePath”);
文件夹=新文件(文件存储位置+“/”+modelFolderName);
如果(!folder.exists()){
folder.mkdirs();
}
try(FileOutputStream fout=newfileoutputstream(文件存储位置+“/”+modelFolderName+“/”+modelFileName)){
try(BufferedInputStream in=新的BufferedInputStream(zipInputStream)){
字节[]缓冲区=新字节[8096];
while(true){
int count=in.read(缓冲区);
如果(计数==-1){
打破
}
四次写入(缓冲区,0,计数);
}
}
}
zipEntry=zipInputStream.getnextery();
}
您正在使用语法try with resource。括号内的所有内容都将自动关闭,就像有一个finally块来关闭它一样
当中的在隐式finally块中关闭时,zipInputStream
也将关闭,因为BufferedInputStream
是FilterInputStream
的子类,它在自身关闭时关闭其源
(一般来说,大多数实现Closable
的类在调用close
时都会释放任何相关的资源)
查看FilterInputStream::close的实现
您是否缺少代码?因为您只检查第一个流条目。绕着拉链圈,不仅仅是一个入口在第二次尝试使用资源时,您正在将zipInputStream
包装到一个BufferedInputStream
中,该流在尝试后关闭,并关闭底层流,该流也用于zipInputStream
,而且您似乎正在try块中使用整个流,而不是只有寻求调试帮助的entryQuestions(“为什么这段代码不工作?”)必须包括所需的行为、特定的问题或错误,以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参见:如何创建。使用链接改进您的问题-不要通过评论添加更多信息。谢谢不要只是把异常信息放在问题的标题中。将异常堆栈跟踪的相关部分添加到问题中,并清楚地确定您的代码抛出哪一行。@GhostCat我添加了更多的解释。考虑到我们这里的信息量,我现在倾向于同意您的意见;-)