Menu
Topics Index
...
`

CharArrayWriterDemo - Java Example Program


CharArrayWriterDemo
import java.io.*;

class CharArrayWriterDemo
{
    public static void main(String arg[])
    {
        CharArrayWriter caw = new CharArrayWriter(); // LINE A
        String s = "I love programming";
        char c[] = s.toCharArray();
        caw.write(c); // LINE B
        System.out.println("Retrieving data from CharArrayWriter : ");
        System.out.println(caw.toString()); // LINE C
        CharSequence cs = "in Java";
        caw.append(cs); // LINE D
        System.out.println("CharArrayWriter after appending CharSequence : ");
        System.out.println(caw);
        caw.append('.'); // LINE E
        System.out.println("CharArrayWriter after appending character: ");
        System.out.println(caw);
        caw.close();    
    }
}
OUTPUT

Retrieving data from CharArrayWriter :
I love programming
CharArrayWriter after appending CharSequence :
I love programmingin Java
CharArrayWriter after appending character:
I love programmingin Java.

DESCRIPTION

At LINE A we created CharArrayWriter.
At LINE B we are writing in to the Stream.
At LINE C we are retrieving data from Stream.
At LINE D we are appending CharSequence data to Stream.
At LINE E we are appending Character to Stream.

THINGS TO TRY
  • Now append "Merit Campus My Online Java School." to caw and print the CharArrayWriter. Since the close method show no effect here your append operation will not throw any I/O Exception.

© meritcampus 2019

All Rights Reserved.

Open In App