
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
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.