OutputStreamWriter close Example
OutputStreamWriter class close example. This example shows you how to use close method.
OutputStreamWriter class close 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
/**
* @ # Close.java
* A class repersenting use to close method
* of OutputStreamWriter class in java.io package
* version 03 June 2008
* author Rose India
*/
import java.io.*;
public class Close {
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();
}
} |
|