androidfilefile-iounbuffered-output

Android: BufferedOutputStream necessary when saving file to internal data?


I wanted to save a string to a file and read it back, so I followed those two examples:

However, in both of them, no BufferedOutputStream wrapper is used, while the docu of FileOutputStream recommends it.

Was this done to have an easier example or is it really not necessary on Android? And does whatever the answer is also apply to the InputStream?

Regards, jellyfish


Solution

  • In this case, the authors of the examples know the size of the output data to be small ahead of time. You really only need the Buffered version if you have "large" amounts of data to write (which you usually don't know for absolute certainty ahead of time).

    The JavaDocs for BufferedOutputStream highlights this well...

    Expensive interaction with the underlying input stream is minimized, since most (smaller) requests can be satisfied by accessing the buffer alone.