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.
Answer from Peter Lawrey on Stack OverflowDeveloper 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.
Help with Java.io.PrintWriter.flush()? Also a bit of a rant.
java - How to use flush() for PrintWriter - Stack Overflow
java - What exactly is the use of flush for a printwriter object? - Stack Overflow
io - Java - Understanding PrintWriter and need for flush - Stack Overflow
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.
So I am confused with the flush() method of the PrintWriter class. According to all documentation descriptions, it 'flushes' the stream, whatever that means. Luckily I got a better explanation somewhere else, but even then it didn't explain how I'd seen it function. GeeksforGeeks said " it means to clear the stream of any element that may be or maybe not inside the stream". Ok, so I assume that just means it empties the stream, yes?
No, that is not what it does. The given code from the GeeksforGeeks website (found here) is as follows:
// Java program to demonstrate
// PrintWriter flush() method
import java.io.*;
class GFG {
public static void main(String[] args)
{
// The string to be written in the Writer
String str = "GeeksForGeeks";
try {
// Create a PrintWriter instance
PrintWriter writer
= new PrintWriter(System.out);
// Write the above string to this writer
// This will put the string in the stream
// till it is printed on the console
writer.write(str);
// Now clear the stream
// using flush() method
writer.flush();
}
catch (Exception e) {
System.out.println(e);
}
}
} With output as follows:
GeeksForGeeks
Simple enough right? Well if you remove line 26 of the code above (writer.flush()) then you get no output. So that means that when you flush the input stream, it actually goes somewhere, it doesn't just get erased. So why isn't that put into the documentation and explanation?
I am upset about this whole thing and I'm annoyed that the official documentation doesn't elaborate more on what 'flush' means in the context of the method, class, etc. But aside from me being upset, I would like someone to either explain whats going on here or to link me to some documentation that actually gets it right.
flush() is probably not required in your example.
What it does is ensure that anything written to the writer prior to the call to flush() is written to the underlying stream, rather than sit in some internal buffer.
The method comes in handy in several types of circumstances:
If another process (or thread) needs to examine the file while it's being written to, and it's important that the other process sees all the recent writes.
If the writing process might crash, and it's important that no writes to the file get lost.
If you're writing to the console, and need to make sure that every message is shown as soon as it's written.
Close method flushes before closing. If you use close(), flush() is not necessary in your first and second example (except u wanna use flush before closing).
On top of that your second example also flushes automatically when using the methods: println, printf, or format
The println() methods of the PrintWriter class "commit" the data to the stream only if the autoFlush attribute is enabled.
Look at this quote from the Java docs:
if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked
Link to Java docs here: https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#PrintWriter(java.io.OutputStream,%20boolean)
If you need to send the message over to the server, then create the PrintWriter object with the autoflush option turned on.
So, where you're creating the writer object, just do this:
writer = new PrintWriter(sock.getOutputStream(), true);
That should do it. Hope it helps!
It's more efficient not to flush the data after every single println. If you're writing a lot of data, you want to buffer it and send in big chunks, not send each line separately. That's why you need to manually flush the streams if you want to make sure that the data is indeed sent to the other end.
Imagine you're writing an email to a friend, and you send each word as its own email vs. sending the whole text in a single email. Which do you think is faster? Sure, your friend will get to read the mail quicker if you send each word as you think of it, but the total time taken will become longer.