Home java Can I flush () on an unbuffered stream and what will happen?

Can I flush () on an unbuffered stream and what will happen?

Author

Date

Category

I’m starting to learn about IO and NIO.

I know that flush () forces the thread to flush its internal buffers.
Can’t figure out if flush () can be used on an unbuffered stream and what will happen?


Answer 1, authority 100%

As follows from the description of flush () :

This method forces any data that may have been buffered to be written
to the underlying output device. Please note that the host environment
might perform its own buffering unbeknowst to Java. In that case, a
write made (for example, to a disk drive) might be cached in OS
buffers instead of actually being written to disk.

Translated into Russian means that flush () causes the buffer to be flushed by calling a method implemented in the OS itself, on top of which the JVM is running. At the same time, even the absence of a buffer in the stream itself does not mean that the disk cache at the OS level or even lower will not be used as a buffer.

In general, flush () is a good form for any stream, since the developer does not know if there is a buffer at the OS level (moreover, the developer usually does not even know which axis will be used).


Answer 2, authority 25%

I think the answer of the respected @Barmaley is not correct. This is my value judgment)

This method forces any data that may have been buffered to be written to the underlying output device. Please note that the host environment might perform its own buffering unbeknowst to Java. In that case, a write made (for example, to a disk drive) might be cached in OS buffers instead of actually being written to disk.

My understanding of the cited documentation is that calling the flush () method only guarantees that the bytes previously written to the stream are transferred to the operating system for writing, but does not guarantee writing to disk.
Here is a link to the answer that I think is correct: FileOutputStream: Does the“ close ”method calls also“ flush ”?

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions