Why use BufferedReader but not BufferedWriter?


#1

To read in the .json file we wrap our InputStreamReader in a BufferedReader:

BufferedReader reader = null; try{ InputStream inputStream = mContext.openFileInput(mFilename); reader = new BufferedReader(new InputStreamReader(inputStream)); ...

But to write to file, we don’t use a BufferedWriter:

Writer writer = null; try{ OutputStream out = mContext .openFileOutput(mFilename, Context.MODE_PRIVATE); writer = new OutputStreamWriter(out); ...

Is there any benefit to wrapping the OutputStreamWriter in a BufferedWriter?


#2

Off the top of my head, I’d say the benefit for BufferedWriter would be equivalent to BufferedReader.

In the example, it’s probably not going to matter because the file is relatively small. For larger files, the buffering may help alleviate some IO performance issues, but I would need to stress test it to be sure.


#3

To add a bit to what Brian said: in our example, we used BufferedReader only because it adds a .readLine() method, which makes our lives a little easier. BufferedWriter provides a corresponding .writeLine(), but we didn’t need it. Writer already has a .write(String) method to write everything at once. As Brian says, if we were writing a larger amount of data, it would be a good idea to use BufferedWriter to write it a bit at a time.