java FileOutputStream 与 ByteArrayOutputStream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4587003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:05:15  来源:igfitidea点击:

FileOutputStream vs ByteArrayOutputStream

javafilestream

提问by Russell

I'm reading somebody else's code. Here's the gist of it.

我正在阅读别人的代码。这是它的要点。

A class compresses and decompresses files using GZIPInputStream and GZIPOutputStream.

一个类使用 GZIPInputStream 和 GZIPOutputStream 压缩和解压缩文件。

Here's a snippet of what goes on during compression. inputFileand outputFileare instances of the class File.

这是压缩过程中发生的事情的片段。inputFile并且outputFile是类的实例File

FileInputStream fis = new FileInputStream(inputFile);
GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(outputFile));

//the following function copies an input stream to an output stream
IOUtils.copy(fis,gzos);

//outputFile is the compressed file
...

Now, here's what's going on during decompression.

现在,这是减压过程中发生的事情。

GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(inputFile));
ByteArrayOutputStream baos = new ByteArrayOutputStream();

//copies input stream to output stream
IOUtils.copy(gzis,baos);

//this method does as its name suggests
FileUtils.writeByteArrayToFile(outputFile, baos.toByteArray());

//outputFile is the decompressed file
...

What's a possible reason the original programmer chose FileOutputStreamduring compression and ByteArrayOutputStreamduring decompression? It confuses me.

原始程序员FileOutputStream在压缩和ByteArrayOutputStream解压期间选择的可能原因是什么?这让我很困惑。

Unless there's a good reason, I think I'm changing them to be consistant to avoid future confusion. Is this a good idea?

除非有充分的理由,否则我认为我正在将它们更改为一致以避免未来混淆。这是一个好主意吗?

回答by Chris Jester-Young

Heh, sounds like they copied and pasted code from different sources? :-P No, seriously, unless you need to inspect the decompressed data, you can just use a BufferedOutputStreamfor both compression and decompression.

嘿,听起来他们从不同来源复制和粘贴代码?:-P 不,说真的,除非您需要检查解压缩的数据,否则您可以只使用 aBufferedOutputStream进行压缩和解压缩。

回答by BalusC

The ByteArrayOutputStreamis more memory hogging since it stores the entire content in Java's memory (in flavor of a byte[]). The FileOutputStreamwrites to disk directly and is hence less memory hogging. I don't see any sensible reason to use ByteArrayOutputStreamin this particular case. It is not modifying the individual bytes afterwards. It just get written unchanged to file afterwards. It's thus an unnecessary intermediate step.

ByteArrayOutputStream是更多的内存粗加工,因为它存储在Java的内存中的全部内容(在的味道byte[])。的FileOutputStream直接写入磁盘并且是存储器拱因此以下。我没有看到ByteArrayOutputStream在这种特殊情况下使用的任何合理理由。之后它不会修改单个字节。之后它只是被原封不动地写入文件。因此,这是一个不必要的中间步骤。

回答by AlexR

The programmer used FileInputStream during compression and used buffer when decompressing. I think that the reason was that if you are failing duinr reading the file nothing bad happens. You just fail and a exception is thrown.

程序员在压缩时使用 FileInputStream,在解压时使用缓冲区。我认为原因是,如果 duinr 读取文件失败,则不会发生任何坏事。您只是失败并抛出异常。

If you are failing while decompressing and you already started writing to file the file is corrupted. So he decided to write buffer first and then when decompression is completed to writer the buffer on disk. This solution is OK if you are dealing with relatively small files. Otherwise this requires to much memory and could produce OutOfMemeoryError.

如果您在解压缩时失败并且您已经开始写入文件,则该文件已损坏。所以他决定先写buffer,解压完成后再写buffer到磁盘。如果您正在处理相对较小的文件,则此解决方案是可以的。否则这需要大量内存并可能产生 OutOfMemeoryError。

I'd extract zip directly to temporaryfile and then rename the temporary file to its permanent name. Finally block should care to delete the temporary file.

我会直接将 zip 解压缩到临时文件,然后将临时文件重命名为其永久名称。最后块应该小心删除临时文件。

回答by sblundy

ByteArrayOutputStreamwould give him/her a nice OutOfMemoryError?

ByteArrayOutputStream会给他/她OutOfMemoryError好吗?

Seriously, they were probably done at different times. If you can, I'd consult the VCS logs.

说真的,他们可能是在不同的时间完成的。如果可以,我会查阅 VCS 日志。