BufferedWriter close Example

BufferedWriter class close example. This example shows you how to use close method.


BufferedWriter class close example.public void close() throws IOExceptionCloses 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 BufferedWriter class in java.io package
 * version 29 May 2008
 * author Rose India 
 */
import java.io.*;

public class Close {

    public static void main(String[] argsthrows Exception {
        // Create a new instance of a BufferedWriter object using
        // a StringWriter.
        StringWriter stringWriter = new StringWriter();
        BufferedWriter bufWriter = new BufferedWriter(stringWriter);

        // Write to the underlying StringWriter.
        String s = "This is the string being written.";

        // Print out the first 12 characters.
        bufWriter.write(s, 012);

        // Print a newline character.
        bufWriter.newLine();

        // Print the rest of the string.
        bufWriter.write(s, 12, s.length() 12);
        bufWriter.flush();
        System.out.println(stringWriter.getBuffer());

        // Close the BufferedWriter object and the underlying
        // StringWriter object.
        stringWriter.close();
        bufWriter.close();
    }
}

Output
This is the
string being written.

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US