Search code examples
javaio

difference between flush and close function in case of filewriter in java


I need to know what is exact difference between flush and close function in Java?

And When the data is dumped into a file during writing of file?

Please provide one example


Solution

  • flush() writes the content of the buffer to the destination and makes the buffer empty for further data to store but it does not closes the stream permanently. That means you can still write some more data to the stream.

    But close() closes the stream permanently. If you want to write some data further, then you have to reopen the stream again and append the data with the existing ones.