简体   繁体   中英

Testing write(byte[]) on FileOutputStream vs. BufferedOutputStream

Is there an actual performance difference when using write(byte[]) methods from FileOutputStream and BufferedOutputStream ?

I tested both on HDD to write 500 MB file and the result was 13 and 12 seconds:

try(FileOutputStream out = new FileOutputStream(filePath1)) {
            out.write(readBytes);
}

and,

try(BufferedOutputStream out = new BufferedOutputStream( 
                           new FileOutputStream(filePath2))) {
            out.write(readBytes);
}

What am I missing about BufferedOutputStream efficiency?

BufferedOutputStream is more efficient if you're writing data a little bit at a time: it batches the writes until it has "enough" data.

If you're writing it all at once, there's going to be no difference, because there's always enough data to fill the buffer; or you've reached the end of the data and need to close the stream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM