1

When you use the flush() method while using a BufferedWriter, that makes sense because you might want to clear the stream immediately so that the person doesn't have to wait until all the writes are done before the file is updated.

But when you are using a FileWriter, what you put into the parameters of write() is written directly to the file, right?

In this case, there doesn't seem to be a buffer therefore flush is useless (in the case of FileWriter). So is there some kind of mini buffer in the FileWriter that I'm overlooking?

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Allan Henriques
  • 175
  • 1
  • 9
  • 1
    `FileWriter implements Flushable` by extending `OutputStreamWriter` and has to implement the method `flush()`, whose [JavaDocs](https://docs.oracle.com/javase/7/docs/api/java/io/Flushable.html#flush()) say *Flushes this stream by writing any buffered output to the underlying stream.*, so maybe in case of *no buffer* it would flush nothing. There might be no point in flushing a `FileWriter`, check the implementation ;-) – deHaar Dec 03 '19 at 07:04
  • 1
    @AnkitKante They are asking about a common Java library, I'm not sure I see the reason to share the code? – Draken Dec 03 '19 at 08:26
  • '... what you put into the parameters of `write()` is written directly to the file, right?': where does it say that? All you know is that it goes into the `OutputStreamWriter`, whose `flush()` method exists rather than being inherited, which suggests that it does something. You don't have any evidence for your belief. – user207421 Dec 03 '19 at 08:53
  • 2
    "*written directly to the file*"? - NO - very first sentence of [documentation](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/io/FileWriter.html) of `FileWriter`: "*Writes text to character files using a default **buffer** size.*" and from documentation of `OutputStreamWriter`, its superclass: "*The resulting bytes are **accumulated in a buffer** before being written to the underlying output stream.*" – user85421 Dec 03 '19 at 10:21
  • 1
    @user85421 Note that the documentation was updated and did not state this that clearly before (Java 8 for example). So might be easy to overlook with the old documentation. – Zabuzard Dec 03 '19 at 11:05
  • 1
    @Zabuza sure, documentation can always be ingn... I mean overlooked (happens too often, based on experience at SO) but still in Java 8 `StreamOutputWriter` states that bytes are buffered, and nowhere `FileWriter` states it writes directly – user85421 Dec 03 '19 at 11:39
  • @Zabuza The Javadoc for `OutputStreamWriter.flush()` and `Writer.flush()` in 1.6 and 1.7 is identical to that for 1.8, and the Javadoc for 1.2 was if anything *more* explicit in my book copy. – user207421 Dec 03 '19 at 23:34

1 Answers1

3

Explanation

But when you are using a FileWriter, what you put into the parameters of write() is written directly to the file, right?

No, the class FileWriter does use a buffer. So the flush method has its valid use-case.

If it would have no bufferring, the method would be useless, yes. But it has buffering.

So flush() must be used to trigger passing the actual data down the stream in case it is still in the internal buffer. Exactly like you already explained, with BufferedWriter for example.


Documentation and source code

First, the documentation clearly states that it uses buffering-logic (the documentation was updated to highlight this, the old documentation did not mention it that clearly):

Writes text to character files using a default buffer size.

And more details in the parent class:

The resulting bytes are accumulated in a buffer before being written to the underlying output stream.

But okay, let us also take a look at the current implementation (Java 13):

private static final int DEFAULT_BYTE_BUFFER_SIZE = 8192;
// ...
private ByteBuffer bb;
// ...
bb = ByteBuffer.allocate(DEFAULT_BYTE_BUFFER_SIZE);
// bb is used in the write and flush method ...

Note that the implementation is platform specific and can vary between releases, OS and especially different JVMs (the StreamEncoder is a sun-class).

Zabuzard
  • 25,064
  • 8
  • 58
  • 82