关闭特定输出流的方法

发布于 2024-12-08 03:36:02 字数 385 浏览 0 评论 0原文

您认为这段代码正确地关闭了输出流条目吗?知道这不是同一类型的输出流。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

相权↑美人 2024-12-15 03:36:02

如果您需要在 out 变量上调用特定于 ZipOutputStream 的方法,则其类型不应该是 OutputStream,而是 ZipOutputStream

ZipOutputStream out = new JarOutputStream(new FileInputstrem(fileName));
                   ...
out.closeEntry();

这不会导致任何问题,因为由于 JarOutputStream 扩展了 ZipOutputStream,所以 JarOutputStream 也是一个 ZipOutputStream< /代码> (也是一个OutputStream,也是一个Object)。

If you need to call methods that are specific to ZipOutputStream on your out variable, then its type should not be OutputStream, but ZipOutputStream:

ZipOutputStream out = new JarOutputStream(new FileInputstrem(fileName));
                   ...
out.closeEntry();

This doesn't cause any problem, because since JarOutputStream extends ZipOutputStream, a JarOutputStream is also a ZipOutputStream (and is also an OutputStream, and is also an Object).

风和你 2024-12-15 03:36:02

由于 JarOutputStream 扩展了 ZipOutputStream,并且 Java 中的所有方法都是虚拟的,因此

((ZipOutputStream) out).closeEntry();

调用完全相同的方法 ass

((JarOutputStream) out).closeEntry();

但是,我建议您这样做静态类型更精确一点:

ZipOutputStream out = new JarOutputStream(new FileInputstrem(fileName));
                   ...
out.closeEntry();

Since JarOutputStream extends ZipOutputStream, and since all methods in Java are virtual, doing

((ZipOutputStream) out).closeEntry();

calls the exact same method ass

((JarOutputStream) out).closeEntry();

However, I'd suggest you make the static type a bit more precise:

ZipOutputStream out = new JarOutputStream(new FileInputstrem(fileName));
                   ...
out.closeEntry();
暖风昔人 2024-12-15 03:36:02

因此就我而言 ((ZipOutputStream)out).closeEntry();是
与 ((JarOutputStream)out).closeEntry() 相同;

这是正确的。没有理由写前者。

And therefore in my case ((ZipOutputStream)out).closeEntry(); was the
same as ((JarOutputStream)out).closeEntry();

That is correct. No reason to write the former whatsoever.

寻梦旅人 2024-12-15 03:36:02
ZipOutputStream.closeEntry();

两者都会

JarOutputStream.closeEntry();

关闭 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()

ZipOutputStream.closeEntry();

and

JarOutputStream.closeEntry();

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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文