android – What is flush () for in Kotlin?

Question:

When storing a file I use FileOutputStream() , then it is possible to use write() , flush() and close() . The first and third methods are self-explanatory, but it's not clear to me what flush() .

Example:

var fileOutputStream = FileOutputStream(file)
fileOutputStream.write(bitmapBytes)
fileOutputStream.flush()
fileOutputStream.close()

Answer:

A Flushable is a data destination that can be flushed. The flush method is invoked to write any buffered output to the underlying stream.

The flush() method is used to flush() the stream by writing any stored output to the underlying stream.

In other words, after recording with the write method, the flush method is called to dump all the data that may have been in the buffer and the file is closed with close .

Fountain:
https://developer.android.com/reference/kotlin/java/io/Flushable

Example where they work with flush :
https://www.tutorialesprogramacionya.com/kotlinparaandroidya/detalleconfecto.php?punto=14&codigo=62&inicio=0

Scroll to Top