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 :(
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 :(
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.
Videos
Developer get into a habit of calling flush() after writing something which must be sent.
IMHO Using flush() then close() is common when there has just been a write e.g.
// write a message
out.write(buffer, 0, size);
out.flush();
// finished
out.close();
As you can see the flush() is redundant, but means you are following a pattern.
I guess in many cases it's because they don't know close() also invokes flush(), so they want to be safe.
Anyway, using a buffered stream should make manual flushing almost redundant.
Whilst close should call flush, it's a bit more complicated than that...
Firstly, decorators (such as BufferedOutputStream) are common in Java. Construction of the decorator may fail, so you need to close the "raw" stream in a finally block whose try includes the decorator. In the case of an exception, you don't usually need to close the decorator (except, for instance, for badly implemented compression decorators). You do typically need to flush the decorator in the non-exception case. Therefore:
final RawOutputStream rawOut = new RawOutputStream(rawThing);
try {
final DecoratedOutputStream out = new DecoratedOutputStream(rawOut);
// ... stuff with out within ...
out.flush();
} finally {
rawOut.close();
}
To top it, decorator close methods are often implemented incorrectly. That includes some in java.io until recently.
Of course, you probably want to use the Execute Around idiom to keep in DRY(ish).
Edit: Since Java 8 you can use try-with-resource statements that should handle everything nicely and concisely. The code will call close on each resource even if unnecessary.
try (
RawOutputStream rawOut = new RawOutputStream(rawThing);
DecoratedOutputStream out = new DecoratedOutputStream(rawOut)
) {
// ... stuff with out within ...
}
Close() always flushes so no need to call.
EDIT: This answer is based on common sense and all the outputstream I encountered. Who is going to implement a close() for a buffered stream without flushing buffer first? There is no harm to call flush right before close(). However, there are consequences if flush() is called excessively. It may defeat underneath buffering mechanism.
The method flush is used to "flush" bytes retained in a buffer. FileOutputStream doesn't use any buffer, so flush method is empty. Calling it or not doesn't change the result of your code.
With buffered writers the method close call explicitly flush.
So you need to call flush when you like to write the data before closing the stream and before the buffer is full (when the buffer is full the writer starts writing without waiting a flush call).
The source code of class FileOutputStream hasn't a custom version of method flush. So the flush method used is the version of its super class OutputStream. The code of flush in OutputStream is the following
public void flush() throws IOException {
}
As you see this is an empty method doing nothing, so calling it or not is the same.
I will write a project that download 4 or 5 files repeatedly. I will write a method(for download files) and my method will be in a loop and download files repeatedly.My method will have a code like this.
Does the close method calls flush, or do I have to use flush before closing?
I recommend to use the NIO.2 API and the try-with-resources statement. This will reduce the amount of code and takes care of flushing and closing the streams:
try (InputStream inputStream = con.getInputStream()){
Files.copy(inputStream, Paths.get("C:\\programs\\TRYFILE.csv"));
}
The topic is a bit confusing since OutputStream.close does indeed not require an automatic flush, but subclasses might specify that. They might also provide a flush method which does nothing (e.g. as the one inherited from OutputStream, which is the case for FileOutputStream). In this case it has no effect to call the flush method, of course, so you can omit it.
If in doubt (if you don't know which subclass you're working with) I guess it's better to call the flush manually.
But again, using the code above this is taken care for you.
As the javadoc says, you don't need to flush yourself. But, it's still good to do, considering your readers, and common sense.
Few experts know the javadoc by heart.
I wouldn't know for sure if the stream will be flushed or not without looking it up,
and I'm probably not alone.
Seeing the explicit flush() call makes this perfectly clear,
and therefore makes the code easier to read.
Furthermore, the method name close() implies nothing about flushing.
It's from the Closeable interface,
and naturally, it says nothing about flushing.
If you don't flush a buffered output stream before closing,
despite wanting to flush it,
you'll be relying on assumptions that may or may not be true.
It would weaken your implementation.
Any assumptions you make, you should somehow pass on to future maintainers. One way to do that is by leaving a comment:
// no need to flush() manually, close() will do it automatically
If you don't leave this comment, future maintainers may have to lookup the javadoc too, if like me they don't have it memorized. But then, why would you write such comment when it's easier and better to just call it yourself now and be done with it.
In short, flushing first before closing is simply following good logic. No need for assumptions and second guesses, and no need to make your readers think.
For output, it is important that we do call flush() and close() because buffered data could be lost, as explained by the first answer here. If your program's output is smalland your writer finishes quickly, it won't make much difference to close() and flush() in my experience.
For input, it won't matter if we don't call close() before the system exits.