BufferedStream 链接 Scala(或 Java)
假设我必须写入二进制文件。我可以使用以下代码
val fos = new FileOutputStream("fileName")
,然后使用
fos.write(bytes)
将其与缓冲流链接起来总是一个好主意吗?如:
val fos = new FileOutputStream("FileName")
val bos = new BufferedOutputStream(fos)
同样的规则适用于 FileInputStream 吗?
最后(在链式版本中)是否有必要关闭fos
?
编辑:找到最后一个问题的答案。没有必要关闭内部流,如此处所述。
Assuming that I have to write to a binary file. I can use the following code
val fos = new FileOutputStream("fileName")
and then use
fos.write(bytes)
Is it always a good idea to chain it with a buffered stream? as in:
val fos = new FileOutputStream("FileName")
val bos = new BufferedOutputStream(fos)
Does the same rule hold for FileInputStream
?
Is it necessary to close fos
in the end (in the chained version)?
EDIT: Found the answer to the last question. It is not necessary to close the inner streams, as mentioned here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
取决于您要写入的数据类型。当您不希望为写入的每个字节调用底层系统(执行实际写入的系统)时,可以使用 BufferedStream,而当您想要写入原始字节(例如在写入时)时,可以使用 FileOutputStream一个图像。
Depends on the type of data you want to write. BufferedStream is meant to be used when you don't want the underlying system (the one that performs the actual write) to be called for every byte written, while FileOutputStream is meant to be used when you want to write raw bytes such as when writing an image.