flush in java.io.FileWriter
Asked Answered
A

9

32

I have a question in my mind that, while writing into the file, before closing is done, should we include flush()??. If so what it will do exactly? dont streams auto flush?? EDIT:

So flush what it actually do?

Armament answered 16/11, 2009 at 14:4 Comment(0)
M
25

Writers and streams usually buffer some of your output data in memory and try to write it in bigger blocks at a time. flushing will cause an immediate write to disk from the buffer, so if the program crashes that data won't be lost. Of course there's no guarantee, as the disk may not physically write the data immediately, so it could still be lost. But then it wouldn't be the Java program's fault :)

PrintWriters auto-flush (by default) when you write an end-of-line, and of course streams and buffers flush when you close them. Other than that, there's flushing only when the buffer is full.

Metacarpal answered 16/11, 2009 at 14:31 Comment(3)
Is that buffer size dynamic/static??Armament
Does that matter? It's Java's buffer, not yours. I looked but didn't find a way to set or change it. I think you have to accept the buffering behavior of streams as given.Metacarpal
PrintWriters don't auto-flush by default. To get one that auto-flushes you have to ask for it explicitly by using the PrintWriter(OutputStream, boolean) constructor.Lynnell
L
22

I would highly recommend to call flush before close. Basically it writes remaining bufferized data into file.

If you call flush explicitly you may be sure that any IOException coming out of close is really catastrophic and related to releasing system resources.

When you flush yourself, you can handle its IOException in the same way as you handle your data write exceptions.

Londonderry answered 16/11, 2009 at 15:0 Comment(0)
R
13

You don't need to do a flush because close() will do it for you.

From the javadoc:

"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."

Revoke answered 16/11, 2009 at 14:5 Comment(1)
The only time I ever use flush is if I'm using the stream in some sort of inter-process communication. For example, maybe one process is writing to a named pipe and the other is reading from it. I don't want to close the file handle to the pipe because I'll write more later but I want to make sure all the data has been flushed to the named pipe so that the other process has it.Revoke
C
4

To answer your question as to what flush actually does, it makes sure that anything you have written to the stream - a file in your case - does actually get written to the file there and then.

Java can perform buffering which means that it will hold onto data written in memory until it has a certain amount, and then write it all to the file in one go which is more efficient. The downside of this is that the file is not necessarily up-to-date at any given time. Flush is a way of saying "make the file up-to-date.

Close calls flush first to ensure that after closing the file has what you would expect to see in it, hence as others have pointed out, no need to flush before closing.

Comport answered 16/11, 2009 at 14:31 Comment(0)
P
3

Close automatically flushes. You don't need to call it.

Pyemia answered 16/11, 2009 at 14:5 Comment(0)
D
1

There's no point in calling flush() just before a close(), as others have said. The time to use flush() is if you are keeping the file open but want to ensure that previous writes have been fully completed.

Duna answered 16/11, 2009 at 14:16 Comment(1)
What it will do for ensuring the file writes been fully completed?Armament
M
1

As said, you don't usually need to flush.

It only makes sense if, for some reason, you want another process to see the complete contents of a file you're working with, without closing it. For example, it could be used for a file that is concurrently modified by multiple processes, although with a LOT of care :-)

Miraculous answered 16/11, 2009 at 14:37 Comment(0)
M
1

FileWriter is an evil class as it picks up whatever character set happens to be there, rather than taking an explicit charset. Even if you do want the default, be explicit about it.

The usual solution is OutputStreamWriter and FileOutputStream. It is possible for the decorator to throw an exception. Therefore you need to be able to close the stream even if the writer was never constructed. If you are going to do that, you only need to flush the writer (in the happy case) and always close the stream. (Just to be confusing, some decorators, for instance for handling zips, have resources that do require closing.)

Munford answered 16/11, 2009 at 19:14 Comment(0)
A
1

Another usecase for flushing in program is writing progress of longrunning job into file (so it can be stopped and restarted later. You want to be sure that data is safe on the drive.

while (true) {
  computeStuff();
  progresss += 1;
  out.write(String.format("%d", progress));
  out.flush();
}
out.close();
Alisiaalison answered 22/10, 2013 at 7:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.