Java.io.StringWriter.close() Method



Description

The java.io.StringWriter.close() method closes a StringWriter but has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

Declaration

Following is the declaration for java.io.StringWriter.close() method.

public void close()

Parameters

NA

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs

Example

The following example shows the usage of java.io.StringWriter.close() method.

package com.tutorialspoint;

import java.io.*;

public class StringWriterDemo {
   public static void main(String[] args) {

      // create a new writer
      StringWriter sw = new StringWriter();

      // create a new sequence
      String s = "Hello world";

      // write a string
      sw.write(s);

      // print result
      System.out.println("" + sw.toString());

      try {
         // close the writer
         sw.close();
      } catch (IOException ex) {
         ex.printStackTrace();;
      }
   }
}

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

Hello World
java_io_stringwriter.htm
Advertisements