Java.io.PrintStream.flush() Method



Description

The java.io.PrintStream.flush() method flushes the stream. This is done by writing any buffered output bytes to the underlying output stream and then flushing that stream.

Declaration

Following is the declaration for java.io.PrintStream.flush() method.

 public void flush() 

Parameters

NA

Return Value

This method does not return a value.

Exception

NA

Example

The following example shows the usage of java.io.PrintStream.flush() method.

 package com.tutorialspoint; import java.io.*; public class PrintStreamDemo { public static void main(String[] args) { String s = "Hello World."; // create printstream object PrintStream ps = new PrintStream(System.out); // print our string ps.print(s); // flush the stream to see the results ps.flush(); // print new string ps.println(); ps.print("This is an example"); // flush to see new string ps.flush(); } } 

Let us compile and run the above program, this will produce the following result −

 Hello World. This is an example 
java_io_printstream.htm
Advertisements