Home Java Example Java Io Java IO StringWriter

Related Tutorials


 
 

Share on Google+Share on Google+

Java IO StringWriter

Advertisement
In this section we will discussed about the StringWriter in Java.

Java IO StringWriter

In this section we will discussed about the StringWriter in Java.

java.io.StringWriter writes the String to the output stream. To write the string this character stream collects the string into a string buffer and then constructed a string. StringWriter provides the two constructors to create objects.

Constructor Detail

Constructor
Description
StringWriter() This is a default constructor used for creating a new StringWriter with the default size of string-buffer.
Syntax : public StringWriter()
StringWriter(int initialSize) This constructor is used for creating a new StringWriter with the specified size of string-buffer size.
Syntax : public StringWriter(int initialSize)

Method Detail

  • append(char c) : This method is used to write the characters with the existing characters into the writer.

    Syntax : public StringWriter append(char c)
     
  • append(CharSequence csq) : This method is used to write the sequence of characters with the existing characters into the writer.

    Syntax : public StringWriter append(CharSequence csq)
     
  • append(CharSequence csq, int start, int end) : This method is used to write the specified portion of a sequence of characters with the existing characters into the writer.

    Syntax : public StringWriter append(CharSequence csq, int start, int end)
     
  • close() : This method is used to close the streams.

    Syntax : public void close() throws IOException
     
  • flush() : This method is used to flush the streams.

    Syntax : public void flush()
     
  • getBuffer() : This method is used to find out the string buffer.

    Syntax : public StringBuffer getBuffer()
     
  • toString() : This method is used to convert buffer' current value to the string.

    Syntax : public String toString()
     
  • write(char[] cbuf, int off, int len) : This method is used to write specified part of an array of characters started from the offset off.

    Syntax : public void write(char[] cbuf, int off, int len)
     
  • write(int c) : This method is used to write a character.

    Syntax : public void write(int c)
     
  • write(String str) : This method is used to write a string.

    Syntax : public void write(String str)
     
  • write(String str, int off, int len) : This method is used to write a specified length of string started from the specified offset.

    Syntax : public void write(String str, int off, int len)

Example

An example is being given here which will demonstrate you about how to use the StringWriter in the Java program. In this example I have created a Java class named JavaStingWriterExample.java where created an object of StringWriter using which a string will be write to the stream.

Source Code

JavaStringWriterExample.java

import java.io.StringWriter;
import java.io.IOException;

public class JavaStringWriterExample
{
   public static void main(String args[])
    {
       String str = "Java StringWriter Example";
       
       try {

                StringWriter sw = new StringWriter();
                sw.write(str);
                StringBuffer sb = new StringBuffer();
                sb = sw.getBuffer();
                System.out.println();
                System.out.println("StringBuffer = "+sb);
                System.out.println("String written by StringWriter = "+ sw);
                System.out.println();
                sw.close();                
             }
        catch (IOException e)
            {
               System.out.println(e);
            }
      }
}

Output

When you will execute the above example you will get the output as follows :

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 21, 2012

Related Tutorials

Discuss: Java IO StringWriter  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com