Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of using flush() close() in Android streams?

I know the general idea but I'm just not sure if it has an effect since the Android api states following: "Flushes this stream. Implementations of this method should ensure that any buffered data is written out. This implementation does nothing."

This implementation does nothing <-- does that mean its useless to do or am I missing something?

like image 953
Warpzit Avatar asked Nov 18 '11 10:11

Warpzit


People also ask

What are flush () and close () used for?

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.

What is the importance of flush ()?

The flush() method of PrintWriter Class in Java is used to flush the stream. By flushing the stream, it means to clear the stream of any element that may be or maybe not inside the stream. It neither accepts any parameter nor returns any value.

What does the flush () method of a stream do?

The Flush method is used when you need to send the contents of the Stream buffer to the associated underlying object. For example, a node or file represented by the URL that is the source of the Stream object. This will ensure that all changes made to the contents have been written.

Which method is used to flush the current stream?

The flush() method of PrintStream Class in Java is used to flush the stream.


1 Answers

If you read extended docs on oracle

http://download.oracle.com/javase/1.4.2/docs/api/java/io/OutputStream.html#flush()

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

The flush method of OutputStream does nothing.

Means, the method itself does nothing, and it is abstract, so any underlying Class can implement its own version of flushing streams and forcing them to write (not waiting in write queue)

But anyway, as Guillaume pointed out, it's good practice to call it anyway. If later on, you replace your stream with another implementation that does use it, then you'll be sorry.

Depending on the OS, flush() does nothing more than force the data to be written to the OS.

and

flush() does nothing on FileOutputStream. It is only useful on buffered streams.

and from javadoc of close(), just to note you don't have to flush stream if you're closing it immediately after.

Close the stream, flushing it first. Once a stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously-closed stream, however, has no effect.

related:
FileOutputStream.close is really slow when writing large file
flush in java.io.FileWriter

like image 151
Marek Sebera Avatar answered Oct 31 '22 05:10

Marek Sebera