示例

public class ClassA {

public void makeStream (OutputStream oStream) {

try {
ZipOutputStream stream = new ZipOutputStream(oStream);
stream.write(7);
//do something...

stream.close();

}
catch (IOException e) {
//stream leaked...
}
}
}



解决方案
为了避免资源泄漏,请确保对每个 java.util.zip.ZipOutputStream 都调用 close()。
注意:此规则适用于所有形式的 ZipOutputStream 构造函数。


public class ClassA {

public void makeStream (OutputStream oStream) {

ZipOutputStream stream = null;
try {
stream = new ZipOutputStream(oStream);
stream.write(7);
//do something...

}
catch (IOException e) {
//do something...

}
finally {
if (stream != null) {
try {
stream.close();

}
catch(IOException e) {
//do something...

}
}
}
}
}