FileOutputStream: Does the "close" method calls also "flush"?
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.
Related videos on Youtube
javauser35
Updated on June 12, 2022Comments
-
javauser35 almost 2 years
I'm really confused about flush and close method.In my code I always close my
FileOutputStream
object. But I want to know that if I have to use flush method here, and where can I use it?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 callsflush
, or do I have to use flush before closing?try { InputStream inputStream = con.getInputStream(); FileOutputStream outputStream = new FileOutputStream("C:\\programs\\TRYFILE.csv"); int bytesRead = -1; byte[] buffer = new byte[4096]; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } } catch(Exception e) { // } finally { outputStream.close(); inputStream.close(); }
Note that the code works well: it download the file successfully. But I'm not sure about using
flush
.-
nbro almost 9 yearsDocumentation serves for a purpose.
-
Raedwald almost 9 yearsClosely related: you need not call
flush()
, but you must call `close() -
Raedwald almost 9 yearspossible duplicate of Using flush() before close()
-
-
nbro almost 9 yearsThis does not answer (directly) the question, that's why I down-voted.
-
Puce almost 9 years@nbro it solves the problem without rising the question about flushing/ closing, so I think the answer is valid.
-
Hacketo almost 9 years@Puce there is no problem here, just a simple question
-
nbro almost 9 yearsNo, this is simply an alternative to the way the OP is doing, it does not answer the question, even if it might solve the problem, I don't know to be honest, but I will remove my down-vote because I don't like to be [__]
-
Puce almost 9 years@Hacketo It's not just a sample, but a concrete use case.
-
javauser35 almost 9 yearsSo,you say for my code; there is no difference between using flush or not? Are you absolutely sure about that?
-
Puce almost 9 years@javauser35 But when accessing the filesystem it's actually recommended to use buffered reader/ writers. But then the BufferedOutputStream calls flush on close automatically.
-
javauser35 almost 9 yearsI looked at the source codes of Fileoutputstream.Yes you are right but there is an finalize method in that class and it has flush() method.But that flush() method comes from superclass and again it is empty and do nothing.Am ı right and do I understand the approach truely?
-
javauser35 almost 9 yearsThanks,it really helped.