If you really want to compare FileWriter with BufferedOutputStream to write a text file, the latter should be faster, since there are fewer I / O operations.
- In the case of
FileWriter each call to the write method will be saved immediately (it is not buffered). - In the case of a
BufferedOutputStream data will be written to disk if the buffer is full (or the buffer is blurred using the flush method).
But if you write text files, you should use Writer ; in this case, we can compare a FileWriter with a BufferedWriter :
Looking at
FileWriter fw = new FileWriter(...)
and
BufferedWriter bw = new BufferedWriter(new FileWriter(...)
you have the same situation regarding the number of input / output operations.
A FileWriter uses a FileOutputStream internally. The reason for using FileWriter is that it automatically uses the default character encoding when writing to a file (for example, the internal Java string is encoded in UTF-8). If you use OutputStream , you must manually encode in each record:
So this example is for BufferedWriter :
bw.write("Hello");
matches this example for a BufferedOutputStream :
bos.write("Hello".getBytes(Charset.forName("utf-8")));
if your default encoding is utf-8 .
An OutputStream deals with (raw) bytes, while a Writer deals with (text) characters.
Beryllium
source share