aspose file tools*
The moose likes I/O and Streams and the fly likes what happens in out.flush()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "what happens in out.flush()?" Watch "what happens in out.flush()?" New topic
Author

what happens in out.flush()?

sasiantony
Greenhorn

Joined: Jan 07, 2001
Posts: 2
Hi there,
following is a code to display the messages entered by a user in the keyboard.

import java.io.*;
public class InputTranslator
{
public static void main(String args[]) throws IOException
{
BufferedReader inStream = new BufferedReader(new InputStreamReader(System.in));
String inputLine;
do
{
System.out.println(">");
System.out.flush();
inputLine=inStream.readLine();
System.out.println("your message is:" +inputLine);
}while(inputLine.length()!=0);
}
}
In this program, even if I comment the line System.out.flush(), I couldn't find the difference in the output.
I exactly want to know what is happening in that statement.
thanks in advance.
sasiantony.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
"sasiantony",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please choose a new name which meets the requirements.
Thanks.

Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
"flush()" causes any remaining characters which are waiting in a stream to actually be sent. Whether there are any characters waiting depends on what has been put into the stream, and what sort of stream it is.
From your example it looks like System.out is not buffered, so characters are being sent as soon as they enter the stream. If you were to wrap System.out in a BufferedWriter, you might see a greater difference when you call "flush()". For example:
sasi antony
Greenhorn

Joined: Jan 10, 2001
Posts: 5
Dear Frank Carver,
thanks a lot for cleared the doubt.
also i have changed the name according to the norms by javaranch.

sasi antony.
Jack patrick
Greenhorn

Joined: Oct 04, 2013
Posts: 1
@Frank, I am still strugling as to why we're using system.out.flush().

you've said ( "flush()" causes any remaining characters which are waiting in a stream to actually be sent ) does it mean that if we don't use flush(), there might be a chance of loosing the data ?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

Well, since Frank posted that reply nearly 13 years ago, it's possible he doesn't have the thread in his watch list any longer. So I'm going to take the liberty of trying to answer you.

There are situations where you might lose un-flushed data, yes. If you were writing to a file, say, and your program crashed before you closed the FileWriter, then you could lose the un-flushed data. Normally you don't need to worry about that because closing the FileWriter (and any other buffered output stream or writer) will automatically flush the buffers.

The other situation which occurs is that there might be something waiting to see the data you're writing. That was the original poster's question, I think. The most likely scenario for this case is where you're sending some data over the network to some other program which is going to do something with the data you send. If you don't flush the socket's output stream then the other program may wait indefinitely for the un-flushed data to arrive.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

And, welcome to the Ranch, Jack!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what happens in out.flush()?