javaperformancebufferedoutputstream

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?


Solution

  • 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.