
- 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 class
Introduction
The Java StringWriter class is a character stream that collects its output in a string buffer, which can then be used to construct a string. Closing a StringWriter has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.
Class declaration
Following is the declaration for Java.io.StringWriter class −
public class StringWriter extends Writer
Field
Following are the fields for Java.io.StringWriter class −
protected Object lock − This is the object used to synchronize operations on this stream.
Class constructors
Sr.No. | Constructor & Description |
---|---|
1 |
StringWriter() This creates a new string writer using the default initial string-buffer size. |
2 |
StringWriter(int initialSize) This creates a new string writer using the specified initial string-buffer size. |
Class methods
Sr.No. | Method & Description |
---|---|
1 |
StringWriter append(char c)
This method appends the specified character to this writer. |
2 |
StringWriter append(CharSequence csq)
This method appends the specified character sequence to this writer. |
3 |
StringWriter append(CharSequence csq, int start, int end)
This method appends a subsequence of the specified character sequence to this writer. |
4 |
void close()
Closing a StringWriter has no effect. |
5 |
void flush()
This method flush the stream. |
6 |
StringBuffer getBuffer()
This method return the string buffer itself. |
7 |
String toString()
This method return the buffer's current value as a string. |
8 |
void write(char[] cbuf, int off, int len)
This method write a portion of an array of characters. |
9 |
void write(int c)
This method write a single character. |
10 |
void write(String str)
This method writes a string. |
11 |
void write(String str, int off, int len)
This method writes a portion of a string. |
Methods inherited
This class inherits methods from the following classes −
- Java.io.Writer
- Java.io.Object
Example - Appending a String
The following example shows the usage of StringWriter append(CharSequence csq) method.
StringWriterDemo.java
package com.tutorialspoint; import java.io.StringWriter; public class StringWriterDemo { public static void main(String[] args) throws Exception { StringWriter sw = new StringWriter(); sw.append("Welcome"); sw.append(" to Java"); System.out.println("Output: " + sw.toString()); } }
Output
Let us compile and run the above program, this will produce the following result−
Output: Welcome to Java
Explanation
The append(CharSequence) method appends two strings.
"Welcome" and " to Java" are added sequentially.
Final output: "Welcome to Java".
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 - Appending a Single Character
The following example shows the usage of StringWriter append(char c) method.
StringWriterDemo.java
package com.tutorialspoint; import java.io.StringWriter; public class StringWriterDemo { public static void main(String[] args) throws Exception { StringWriter sw = new StringWriter(); sw.append('H'); sw.append('i'); System.out.println("Output: " + sw.toString()); } }
Output
Let us compile and run the above program, this will produce the following result−
Output: Hi
Explanation
Appends 'H' and 'i' one by one to the StringWriter.
The resulting output is "Hi".