Java - StringWriter close() method



Description

The Java 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 - Usage of StringWriter close() method

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

StringWriterDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.StringWriter;

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

      // 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());

      // close the writer
      sw.close();
   }
}

Output

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

Hello World

Example - Using close() after writing

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

StringWriterDemo.java

package com.tutorialspoint;

import java.io.StringWriter;

public class StringWriterDemo {
   public static void main(String[] args) {
      try {
         StringWriter sw = new StringWriter();
         sw.write("Hello, world!");
         sw.close();  // Safe to call

         // Still usable even after close
         sw.write(" Still writing!");
         System.out.println("Output: " + sw.toString());
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Output

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

Output: Hello, world! Still writing!

Explanation

  • close() is called, but the StringWriter remains usable.

  • This is unlike FileWriter, where close() would make further writes illegal.

Example - Using try-with-resources

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

StringWriterDemo.java

package com.tutorialspoint;

import java.io.StringWriter;

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

      try (StringWriter sw = new StringWriter()) {
         sw.write("Try-with-resources example.");
         result = sw.toString();  // Still works after auto-close
      } catch (Exception e) {
         result = "Error occurred.";
      }

      System.out.println("Output: " + result);
   }
}

Output

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

Output: Try-with-resources example.

Explanation

  • StringWriter is used in a try-with-resources block.

  • close() is automatically called at the end.

  • Still, its content is accessible because close() does nothing harmful.

java_io_stringwriter.htm
Advertisements