Delphi Clinic C++Builder Gate Training & Consultancy Delphi Notes Weblog Dr.Bob's Webshop
Hubert Klein Ikkink (aka Mr.Haki) - Communications Officer
 Mr.Haki's JBuilder Jar #22
See Also: JBuilder Papers

Flushing the output stream
When using outputstream and writer classes from the java.io package, we have to remember to flush the output when we need it. For example we are developing a servlet, which will return HTML to the web browser. Now when we are creating a very long page, we already want to send a part of this page to the browser, so the user will know something is still happening. Because else we have just to wait until the complete page is finished before we see anything in the web browser.

If for example we have created a PrintWriter object to send HTML to the browser, we can use the flush() method available for this object to send all HTML available so far in the writer object to the web browser:

  /*
   * Create writer object with name out
   */
  PrintWriter out = new PrintWriter(response.getOutputStream());
  /*
   * Create HTML text to be send to the browser
   */
  out.println("<H2>Sample</H2>");
  out.println("First line of output.");
  /*
   * Send HTML text so far collected to the browser
   */
  out.flush();
  /*
   * Another line of HTML text
   */
  out.println("Second line of output.");
  /*
   * Flush the last line and close writer object
   */
  out.flush();
  out.close();


This webpage © 1997-2009 by Bob Swart (aka Dr.Bob - www.drbob42.com). All Rights Reserved.