OutputStreamWriter flush Example
OutputStreamWriter class flush example. This example shows you how to use flush method.
OutputStreamWriter class flush example.public void close() throws IOException Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.
Here is the code
/**
* @ # Flush.java
* A class repersenting use to flush method
* of OutputStreamWriter class in java.io package
* version 03 June 2008
* author Rose India
*/
import java.io.*;
public class Flush {
public static void main(String args[]) throws Exception {
// Create a new instance of a OutputStreamWriter object
// attached to a ByteArrayOutputStream.
ByteArrayOutputStream out =
new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out);
// Write to the output stream.
String s = "Random String";
writer.write(s);
writer.flush();
// Display the contents of the ByteArrayOutputStream.
System.out.println(out.toString());
// Close the OutputStreamWriter object.
writer.close();
}
} |
|