关闭特定输出流的方法
您认为这段代码正确地关闭了输出流条目吗?知道这不是同一类型的输出流。
OutputStream out = new JarOutputStream(new FileInputstrem(fileName));
...
((ZipOutputStream)out).closeEntry();
但考虑到两者都是输出流,我认为它们以相同的方式关闭。因此,在我的情况下 ((ZipOutputStream)out).closeEntry();
与 ((JarOutputStream)out).closeEntry();
您能否确认,如果你认为是对的还是纠正我,如果错了? 谢谢。
Do you think this code close correctly the output stream entry ? Knowing that this is not the same type of output stream.
OutputStream out = new JarOutputStream(new FileInputstrem(fileName));
...
((ZipOutputStream)out).closeEntry();
But considering that the both are output stream, I thought they were closing in the same way. And therefore in my case ((ZipOutputStream)out).closeEntry();
was the same as ((JarOutputStream)out).closeEntry();
Can you confirm that if you think is true or correct me if is wrong ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您需要在
out
变量上调用特定于ZipOutputStream
的方法,则其类型不应该是OutputStream
,而是ZipOutputStream
:这不会导致任何问题,因为由于
JarOutputStream
扩展了ZipOutputStream
,所以JarOutputStream
也是一个ZipOutputStream< /代码> (也是一个
OutputStream
,也是一个Object
)。If you need to call methods that are specific to
ZipOutputStream
on yourout
variable, then its type should not beOutputStream
, butZipOutputStream
:This doesn't cause any problem, because since
JarOutputStream
extendsZipOutputStream
, aJarOutputStream
is also aZipOutputStream
(and is also anOutputStream
, and is also anObject
).由于
JarOutputStream
扩展了ZipOutputStream
,并且 Java 中的所有方法都是虚拟的,因此调用完全相同的方法 ass
但是,我建议您这样做静态类型更精确一点:
Since
JarOutputStream
extendsZipOutputStream
, and since all methods in Java are virtual, doingcalls the exact same method ass
However, I'd suggest you make the static type a bit more precise:
这是正确的。没有理由写前者。
That is correct. No reason to write the former whatsoever.
两者都会
关闭 ZIP 条目,因此您可以将另一条目写入存档文件中(如果您想将多个文件存储到一个 ZIP/JAR 中)。它们本身不会关闭输出流。如果您想关闭 JarOutputStream 和底层 FileOutputStream,请使用
关闭();
参见:
http://download.oracle .com/javase/1.4.2/docs/api/java/util/zip/ZipOutputStream.html#close()
http://download.oracle .com/javase/1.4.2/docs/api/java/util/zip/ZipOutputStream.html#closeEntry()
and
both close the ZIP entry, so you can write another entry into the archive file (if you want to store more than one file into one ZIP/JAR). They do not close the output stream itself. If you want to close your JarOutputStream and the underlying FileOutputStream, use
out.close();
Cf.:
http://download.oracle.com/javase/1.4.2/docs/api/java/util/zip/ZipOutputStream.html#close()
http://download.oracle.com/javase/1.4.2/docs/api/java/util/zip/ZipOutputStream.html#closeEntry()