15

StringWriter has a flush function. What does it mean to flush a string buffer?

ceving
  • 21,900
  • 13
  • 104
  • 178

2 Answers2

26

It must have an implementation of flush() because in its superclass Writer that's an abstract method, and StringWriter is not an abstract class. However, if you look at the source code of StringWriter.flush(), which you can find in the file src.zip in your JDK installation directory, you'll see:

/**
 * Flush the stream.
 */
public void flush() {
}

In other words, it does nothing. (There are ofcourse other subclasses of Writer where flush() does do something useful).

The person who implemented this method could have documented that it doesn't do anything, but they didn't.

Jesper
  • 202,709
  • 46
  • 318
  • 350
2

In general a flush forces a buffer to write its content to the destination, for example writing the bytes to the file on the harddisk.

Stefan
  • 2,603
  • 2
  • 33
  • 62