FileWriter vs BufferedWriter

I would like to know if FileWriter is buffered.

In this SO question, it looks like it is, however, in this SO question, it seems that (this will be a system call for every write (..) call.

So basically, after reading these two Q&A, I'm a little confused. Can anyone clearly explain this?

Thanks in advance.

EDIT : The problem is solved by reading this API, which I quote from the relevant part:

Each call to the write () method causes the encoding converter to be called on the specified character (s). The resulting bytes are accumulated in the buffer before being written to the original output stream. The size of this buffer can be specified, but by default it is large enough for most purposes. Note that characters passed to write () are not buffered.

For maximum efficiency, consider porting OutputStreamWriter within a BufferedWriter to avoid frequent conversions. For example:

Writer out = new BufferedWriter (new OutputStreamWriter (System.out));

Since FileWriter extends OutputStreamWriter, it also applies to it.

Thank you for your time, but I know that I asked for something very specific.

+8
source share
2 answers

I would advise always using BufferedWriter. It allows you to control the actual buffer size, and you can guarantee that regardless of the JVM used, IO will be buffered, which will increase IO performance.

0
source

FileWriter not buffered, you should use BufferedWriter as a shell:

 final int myBufferSize = 2048; Writer myWriter = new BufferedWriter(new FileWriter, myBufferSize); 
-1
source

Source: https://habr.com/ru/post/950426/


All Articles