Flushing streams

8 July 2008

Flushing outputstreams has its own importance. You will realize this when working on socket programming.

If you do not flush the output stream every time you write something on it, the data may not actually get written out to the socket, and the two programs will keep on waiting for the data forever.

You can just call flush() after you write something important:

// OutputStream out;
// byte[] data
out.write(data);
out.flush();

If you’re writing text data, you might use a PrintWriter for output. PrintWriter has a special constructor that lets you specify if the stream should be flushed after every newline:

PrintWriter out = new PrintWriter(rawOut, true);

A PrintWriter created in this way will automatically flush itself whenever you write a line of text.

del.icio.us:Flushing streams  digg:Flushing streams  spurl:Flushing streams  wists:Flushing streams  simpy:Flushing streams  newsvine:Flushing streams  blinklist:Flushing streams  furl:Flushing streams  reddit:Flushing streams  fark:Flushing streams  blogmarks:Flushing streams  Y!:Flushing streams  smarking:Flushing streams  magnolia:Flushing streams  segnalo:Flushing streams  gifttagging:Flushing streams

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Flushing streams

Leave a Reply