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