Java.io.StringWriter class in Java
Last Updated :
21 May, 2023
Java StringWriter class creates a string from the characters of the String Buffer stream. Methods of the StringWriter class in Java can also be called after closing the Stream as this will raise no IO Exception.
Declaration in Java StringWriter Class
public class StringWriter
extends Writer
Constructors in StringWriter Class in Java
Constructors used in String Writer Class in Java are mentioned below:
- StringWriter() : Creates a new StringWriter using the initial or default size of String Buffer
- StringWriter(int size) : Creates a new StringWriter using the mentioned size of String Buffer
Methods in Java StringWriter Class
All the methods involved with the Java StringWriter class are mentioned below:
Method
| Description
|
---|
append(char Sw) | appends a single character to the String Buffer. |
append(CharSequence char_sq) | appends specified character sequences to the String Buffer. |
append(CharSequence ,char_sq,int start,int end) | appends specified part of a character sequence to the String Buffer. |
flush() | flushes the StringWriter stream. |
getBuffer() | returns the StringBuffer of StringWriter. |
write(int char) | writes a single character to the String Buffer. |
write(String str) | writes string to the String Buffer. |
write(String str, int offset, int maxlen) | writes some part of the string to the String Buffer. |
write(char[] carray, int offset, int maxlen) | writes some part of the character array to the String Buffer. |
toString() | returns value of the String Buffer as string |
close() | closes String Buffer.Since, method can also be called after closing the String Buffer so,it play no role. |
1. write(int char)
java.io.StringWriter.write(int char) writes a single character to the String Buffer.
Syntax: public void write(int char)
Parameters :
char : int value of the character to be written.
Return :
void
2. write(String str)
java.io.StringWriter.write(String str) writes string to the String Buffer.
Syntax: public void write(String str)
Parameters :
str : string to be written to the String Buffer.
Return :
void
3. write(String str, int offset, int maxlen)
java.io.StringWriter.write(String str, int offset, int maxlen) writes some part of the string to the String Buffer.
Syntax: public void write(String str, int offset, int maxlen)
Parameters :
str : string to be written to the String Buffer.
offset : start position of the String
maxlen : maximum length upto which string has to written
Return :
void
4. write(char[] carray, int offset, int maxlen)
java.io.StringWriter.write(char[] carray, int offset, int maxlen) writes some part of the character array to the String Buffer.
Syntax: public void write(char[] carray, int offset, int maxlen)
Parameters :
carray : character to be written to the String Buffer
offset : start position of the character array
maxlen : maximum no. of the character of the carray has to written
Return :
void
5. toString()
java.io.StringWriter.toString() returns the value of the String Buffer as a string .
Syntax: public String toString()
Parameters :
-----------
Return :
string value of the String Buffer
6. close()
java.io.StringWriter.close() closes String Buffer. Since, method can also be called after closing th String Buffer so, it play no role.
Syntax: public void close()
Parameters :
-----------
Return :
void
7. append(char Sw)
java.io.StringWriter.append(char Sw) appends a single character to the String Buffer.
Syntax: public StringWriter append(char Sw)
Parameters :
Sw : character to be append
Return :
StringWriter
8. append(CharSequence char_sq)
java.io.StringWriter.append(CharSequence char_sq) appends specified character sequence to the String Buffer.
Syntax: public StringWriter append(CharSequence char_sq)
Parameters :
char_sq : Character sequence to append.
Return :
StringWriter, if char sequence is null, then NULL appends to the StringWriter.
9. append(CharSequence char_sq, int start, int end)
java.io.StringWriter.append(CharSequence char_sq, int start, int end) appends specified part of a character sequence to the String Buffer.
Syntax: public StringWriter append(CharSequence char_sq, int start, int end)
Parameters :
char_sq : Character sequence to append.
start : start of character in the Char Sequence
end : end of character in the Char Sequence
Return :
void
10. flush()
java.io.StringWriter.flush() flushes the StringWriter stream.
Syntax: public void flush()
Parameters :
-----
Return :
void
11. getBuffer()
java.io.StringWriter.getBuffer() returns the String Buffer of StringWriter.
Syntax: public StringBuffer getBuffer()
Parameters :
-----------
Return :
String Buffer with its current value.
Example
Java program illustrating the use of StringWriter class methods :
Java
// Java program illustrating the working of StringWriter
// class methods write(int char), write(String str), close()
// write(String str, int offset, int maxlen), toString()
// write(char[] carray, int offset, int maxlen)
import java.io.*;
public class NewClass {
public static void main(String[] args)
throws IOException
{
// Initializing String Writer
StringWriter geek_writer1 = new StringWriter();
StringWriter geek_writer2 = new StringWriter();
StringWriter geek_writer3 = new StringWriter();
StringWriter geek_writer4 = new StringWriter();
// Use of write(int char) : to write a character
geek_writer1.write(71);
geek_writer1.write(70);
geek_writer1.write(71);
// Use of toString() : Value written by write(int
// char)
System.out.println("Using write(int char) : "
+ geek_writer1.toString());
String str = "Hello Geeks";
// Use of write(String str) : to write string
geek_writer2.write(str);
// Value written by write(String str)
System.out.println("Using write(String str) : "
+ geek_writer2.toString());
// Use of write(String str, int offset, int maxlen)
// : to write part of string
geek_writer3.write(str, 2, 4);
geek_writer3.write(str, 5, 6);
// Value written by write(String str, int offset,
// int maxlen)
System.out.println(
"Using write(str, offset, maxlen) : "
+ geek_writer3.toString());
try {
// Use of Close() method : to close the
// geek_writer4 But no use as String Writer
// method is still working with it
geek_writer4.close();
}
catch (IOException except) {
System.out.println("ERROR");
except.printStackTrace();
;
}
// Initializing character array
char[] carray = str.toCharArray();
// Use of write(char[] carray, int offset, int
// maxlen): to write part of char array
geek_writer4.write(carray, 4, 6);
// Value written by write(String str, int offset,
// int maxlen)
System.out.println(
"Using write(carray, offset, maxlen) : "
+ geek_writer4.toString());
}
}
Output
Using write(int char) : GFG
Using write(String str) : Hello Geeks
Using write(str, offset, maxlen) : llo Geeks
Using write(carray, offset, maxlen) : o Geek
Java program illustrating use of StringWriter class methods : append(CharSequence char_sq), append(char Sw), append(CharSequence char_sq, int start,int end), flush(), getgetBuffer()
Java
// Java program illustrating the working of StringWriter
// class methods append(CharSequence char_sq), append(char
// Sw) append(CharSequence char_sq, int start,int end)
// flush(), getgetBuffer()
import java.io.*;
public class NewClass {
public static void main(String[] args)
throws IOException
{
// Initializing String Writer
StringWriter geek_writer1 = new StringWriter();
StringWriter geek_writer2 = new StringWriter();
StringWriter geek_writer3 = new StringWriter();
// Use of write(int char) : to write a character
geek_writer1.write(71);
geek_writer1.write(70);
geek_writer1.write(71);
// Use of toString() : Value written by write(int
// char)
System.out.println("Using write(int char) : "
+ geek_writer1.toString());
// Using flush() method
geek_writer1.flush();
System.out.println("Using flush() : "
+ geek_writer1.toString());
// Use of append(char Sw)
System.out.println("append(char Sw) : "
+ geek_writer1.append(" 1GFG1"));
// Use of getBuffer() :
System.out.println("Using geek_writer1 : "
+ geek_writer1.getBuffer());
// Initializing Character Sequence
CharSequence char_sq1 = "1 Hello 1";
CharSequence char_sq2 = " : 2 Geeks 2";
// Use of append(CharSequence char_sq)
geek_writer2.append(char_sq1);
geek_writer2.append(char_sq2);
System.out.println("append(char_sq) : "
+ geek_writer2.toString());
// Use of append(CharSequence char_sq,int start,int
// end)
geek_writer3.append(char_sq1, 0, 3);
geek_writer3.append(char_sq2, 3, 6);
System.out.println("append(char_sq,start,end) : "
+ geek_writer3.toString());
}
}
Output
Using write(int char) : GFG
Using flush() : GFG
append(char Sw) : GFG 1GFG1
Using geek_writer1 : GFG 1GFG1
append(char_sq) : 1 Hello 1 : 2 Geeks 2
append(char_sq,start,end) : 1 H2 G
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
TCP/IP Model The TCP/IP model (Transmission Control Protocol/Internet Protocol) is a four-layer networking framework that enables reliable communication between devices over interconnected networks. It provides a standardized set of protocols for transmitting data across interconnected networks, ensuring efficie
7 min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read