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

JavaIo

Java Problem Overview


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

Java Solutions


Solution 1 - Java

flush() just makes sure that any buffered data is written to disk (in this case - more generally, flushed through whatever IO channel you're using). You can still write to the stream (or writer) afterwards.

close() flushes the data and indicates that there isn't any more data. It closes any file handles, sockets or whatever. You then can't write to the stream (or writer) any more.

Note that without calls to flush() data can still be written to the IO channel in question - it's just that some data might be buffered.

close() generally calls flush() as well, but it's recently been pointed out to me that in some JDK implementations, any exceptions thrown by flushing as part of closing are swallowed :(

Solution 2 - Java

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.

Solution 3 - Java

flush() flushes content of buffer to destination.And you can write something again into the stream.close() flushes content to destination and closes the stream. After close() you can't write anything anymore.

Solution 4 - Java

flush() :To flush output stream, use void flush() method of DataOutputStream class. This method internally calls flush() method of underlying OutputStream class which forces any buffered output bytes to be written in the stream.

close() :It generally close the Stream,connection,socket.

See flush and close example

Solution 5 - Java

The flush method will flush all the content in the streams to the destination. After writing something, it may not be at the destination.

The close method will close the streams and will do the flush before closing.

The following is I got from [Oracle Java I/O tutorial]. >To flush a stream manually, invoke its flush method. The flush method is valid on any output stream, but has no effect unless the stream is buffered.

If you only use fileWriter without buffer, using flush has no effect. The only thing you need to do is close the streams. And the close method will flush the streams before closing them.

If I understand correctly, it seems that as long as we remember to close the streams, we are good. [Oracle Java I/O tutorial]:https://docs.oracle.com/javase/tutorial/essential/io/buffers.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmumView Question on Stackoverflow
Solution 1 - JavaJon SkeetView Answer on Stackoverflow
Solution 2 - JavaChandra SekharView Answer on Stackoverflow
Solution 3 - Javashift66View Answer on Stackoverflow
Solution 4 - JavaSureshView Answer on Stackoverflow
Solution 5 - JavaMIKECView Answer on Stackoverflow