×

Java StringWriter Class

The StringWriter class is a character stream in which it is used to store the output consisting of characters into the string buffer. Upon collecting output into a string buffer, a string is further constructed.

The string buffer in java is a mutable string that means it can be modified.

Java StringWriter class allows you to get the characters written to the writer as a whole string.

The StringWriter class is used if we want the data as a whole string or if you have a component that consists of characters that can only write data to a writer, but you want the data to be a string.

After finishing writing characters to the java StringWriter, we can always choose to close the string buffer stream. But the String writer class is not connected to any system resources like network connections or other forces,it is not always important to close the stream.

Package

The Java StringWriter class is available in the java.io package. To use this String Writer class, the java.io.StringWriter package needs to be imported.

In java IO classes, the super class of string writer is Writer class and the super class of this Writer class is object class. Using string writer classes, closeable, auto-closeable, appendable, and flushable interfaces are implemented.

Constructor

The string writer class supports two types of constructors, one for default and one for finding the initial size of the string buffer. They are 

  1. StringWriter() – it is a default constructor and takes the default initial size of string buffer to create a new string writer
  2. StringWriter(int initialsize) – this constructor is used for creating a new string writer by taking a specific initial size of string buffer.

Methods

Different types of methods present in Writer class are implemented using the StringWriter class. Few methods are mentioned below.

String() methods

  • String toString() –  for converting the string buffer present available current value as a string 
  • StringBuffer getBuffer() –to get string buffer this particular method can be used

Append() methods

  • StringWriter append(CharSequence csq) –for appending the particular character sequence to the writer
  • StringWriter append(char c) – same as the above method, but this method is used for appending only characters to the writer

Write() methods

  • Write()- for writing only a single character to the writer, write () method is helpful
  • Write(char[] array)- this method is used for writing specific characters present in an array to the writer
  • Write(String data) – a specified whole string is written to the writer in this method 

Other methods

  • Void close() -this method is for closing the stream
  • Void flush()- this method is for flushing the stream. That means the data present in the writer is forced into the string buffer.

Declaration of StringWriter class

The writer class is extended by a string writer class. The declaration of string writer class can be given as:

public class StringWriter extends Writer

Creation of StringWriter class

To create a string writer class, the java.io.StringWriter package needs to be imported in the first place. Once the package is imported, then the class can be implemented in the below following two cases.

  • With default initial buffer size
 StringWriter result  =  new StringWriter();
  • With initial size of string buffer
 StringWriter result  =  new StringWriter(int size);

Example Java Program for StringWriter class:

// This program is for demonstrating a String Writer class
// import the package
Import java.io. *;
Import java.io.StringWriter;
Public class StringDemo
{
Public static void main(String s [])
{
// Creation of string writer class
StringWriter charStream = new StringWriter (); 
// Creation of print writer class
PrintWriter outputStream = new PrintWriter (charStream);
String s1 = “My name is tony stark ”; // declare any string s1
String s2 = “I am Avengers team”; // declare any string s2
Int num = 5; // take some integer value
// Now print the string sequence using the output stream object
outputStream. print( s1 + “ “ + s2 + “ “ + “we are fantastic ” + num ); 
String result = charStream. toString(); // assign result
System. out. println (result); // print result
}
}

Output:

My name is tony stark I am Avengers team we are fantastic 5

The above result shows us how the data is taken from the file and converted into a string using the toString() method. StringWriter class collects the output from the outputstream and stores it in string buffer, then joins to construct a string.


Related Topics

Java Math max() Method

The max() method of Math class returns the greater of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double max(double a, double b)public...

2 minutes read.

Java Thread Priority in Multithreading

As we realise, java, being object-situated, works inside a multithreading climate in which the string scheduler relegates the processor to a string in light of the need for a string....

6 minutes read.

Java Anon Proxy

The Java Anon Proxy (JAP), also known as JonDonym, is a proxy system designed to enable Web browsing with revocable (the use of or publication under a pseudonym, a false...

4 minutes read.

How to Assign Static Value to Date in Java?

In this tutorial, we will learn certain ways to assign a static value to a date in java. We will see the limitation of each method that will lead to...

3 minutes read.

Java Math random() Method

The random() method of Math class returns a double value with a positive sign, less than 1 and greater than or equal to 0.0. This method is properly synchronized with...

1 minute read.

Java Volatile Keyword

The compiler, runtime, or processors may use any kind of optimization if there aren't any required synchronizations. Although most of the time these improvements are advantageous, they occasionally can result...

6 minutes read.

Java Swings

Swing is a Java Foundation Class library and an extension to the Abstract Window Toolkit (AWT) (JFC).As compared to AWT, Swing has significantly better functionality, new components, increased component features,...

9 minutes read.

Java If Keyword

Definition: The if statement specifies a section of Java code that will run if an if statement's condition is false. The following conditional statements can be used in Java: To provide a block...

3 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

Java Developer

Who is a Java Developer? A Java developer is a skilled programmer who works on commercial applications, software, and webpages.  Java developers can work in two different areas: Operating system development:...

3 minutes read.

How to sort a String in Java

Sorting is the process of putting the elements in a certain order, either ascending or descending. Mostly the alphabetical order or natural order is used for a string. In other...

5 minutes read.

Construct the Largest Number from the Given Array in Java

In this section, we will create a Java programme that will enable you to locate the greatest integer in an array. The programme will begin comparing the array's numbers with...

3 minutes read.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.

Character Array in Java

A character array is an array which holds values of character data types. It is different from a string array. The character array, string, and StringBuffer classes in the Java...

4 minutes read.

Add Time in Java

Before Java 8, java.util.Date was one of the most usually involved classes for addressing date-time values in java. Then Java 8 presented java.time.LocalDateTime and java.time.ZonedDateTime. Java 8 likewise permits us...

6 minutes read.

Deadlock Prevention and avoidance in Java

This tutorial will discuss deadlock prevention and Avoidance in Java programming language. Introduction Multithreading in Java includes deadlock. We can multitask by running several threads concurrently in the multithreading environment. Deadlock occurs...

4 minutes read.

Java Integer toBinaryString() method

The toBinaryString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 2. Syntax public static String toBinaryString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Loose Coupling in Java

Loosely coupling mechanism in java means one reference of a variable capable of holding multiple implementation class memory is called loosely coupling. Or in other words, one interface reference variable...

3 minutes read.

Java While Keyword

Depending on a specified Boolean condition, a while loop in Java allows code to be executed repeatedly. The while loop can be viewed as an iterative version of the if...

3 minutes read.