Java.io - SPLessons

Java.io StringWriter

Home > Lesson > Chapter 51
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io StringWriter

Java.io StringWriter

shape Introduction

The Java.io StringWriter is the class used to write the character stream in the form of string which acts as a destination to string writer. The Java.io StringWriter is a subclass to the writer class, it overrides all the methods from writer class.

Class Declaration

shape Declaration

Java.io StringWriter class is declared below: Public class StringWriter extends Writer

shape Fields

Java.io StringWriter class fields are:
  • Protected Object Lock : For synchronize operations this object is used in StringWriter.
  • Class Methods And Constructors

    shape Table

    Constructor Description
    String writer() The function of this constructor is to create the string writer instance and used to write from the character stream.
    String writer(int initialSize) The function of this constructor is to create the string writer with represented size of the string buffer.
    Method Description
    void close () The function of this method is to close the present stream and associated to this stream, systems resources are released without any impact.
    StringWriter append(char c) The function of this method is to append the given character to the writer.
    void write (Char[] cbuf, int off, int n) By using this method it writes the data up to ‘n’ length from the output stream to an array of Characters.
    void write (int c) The function of this method is to write the represented character into output stream.
    void write (String str,  int off, int n) The function of this method is to write the string up to 'n' length.

    Inherited Methods

    shape Description

    From the following classes, methods are inherited to the string writer class.
  • Java.io.object
  • Java.io.Writer
  • shape Example

    Following is an example. [java]package com.cp.io; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.StringWriter; public class StringWriterDemoOne { public static void main(String[] args) throws IOException { char[] buff = new char[1024]; StringWriter sw = new StringWriter(); FileInputStream fis = null; BufferedReader bufferReader = null; try { fis = new FileInputStream("E:/splesson.txt"); bufferReader = new BufferedReader(new InputStreamReader(fis, "UTF-8")); int n; while ((n = bufferReader.read(buff)) != -1) { sw.write(buff, 0, n); } System.out.println(sw.toString()); } catch (IOException e) { e.printStackTrace(); }finally{ sw.close(); bufferReader.close(); } } } [/java] The E:/splesson.txt file will contain the data that Hello Java.Java IO is fun. Output Following is the result will be displayed. [java] Hello Java.Java IO is fun. [/java] Following is an another example to StringWriter with StringBuffer. [java]package com.cp.io; import java.io.StringWriter; public class StringWriterDemoOne { public static void main(String[] args) { String str1 = "Hello World!"; String str2 ="\nThis is StringReader Program."; StringWriter outputWriter = new StringWriter(); outputWriter.write(str1,0,5); System.out.println(outputWriter.toString()); //get StringBuffer of outputWriter StringBuffer sbuf = outputWriter.getBuffer(); //append str2 into StringBuffer sbuf.append(str2); System.out.println(outputWriter.toString()); } }[/java] StringWriter writes character stream into a StringBuffer and finally this buffer is converted into string. Output The result will be as follows. [java] Hello Hello This is StringReader Program. [/java]

    Summary

    shape Key Points

    • The string writer is the class used to write the character stream in the form of string which acts as a destination to string writer.
    • The string writer is a subclass to the writer class, it overrides all the methods from writer class.