OutputStream flush() Example
Flushes this output stream and forces any buffered output bytes to be written out.
OutputStream class flush() method example. This example shows you how to use flush() method.This method Flushes this output stream and forces any buffered output bytes to be written out.
Here is the code:-
/**
* @Program that Flushes this output stream and forces any buffered output bytes
* to be written out
* Flush.java
* Author:-RoseIndia Team
* Date:-06-Jun-2008
*/
import java.io.*;
public class Flush extends OutputStream {
public void write(int i) throws IOException {
}
public static void main(String[] args) throws Exception {
//creating outputstream object
OutputStream c = new PipedOutputStream();
//creating inputstream object
PipedInputStream pis = new PipedInputStream();
//Connecting to the outputStream
pis.connect((PipedOutputStream) c);
byte[] b = {1, 2, 3, 4, 5};
//Writes bytes from the specified byte array to this output stream.
c.write(b);
//Flushes this output stream
c.flush();
System.out.println("Stream before closing is");
while (pis.available() != 0) {
System.out.println(pis.read());
}
// Closes this output stream
c.close();
System.out.println("Stream after closing is");
System.out.print(pis.read());
}
} |
Output of the Program
Stream before closing is
1
2
3
4
5
Stream after closing is
-1 |
|