Menu
Topics Index
...
`


Input/Output: Exploring java.io > Character Streams >
Siva Nookala - 15 Apr 2016
CharArrayWriter is a subclass of Java Writer Class. It implements a character buffer that can be used as an Writer. The buffer automatically grows when data is written to the stream. The data can be retrieved using toCharArray() and toString().
Note: Invoking close() on this class has no effect, and methods of this class can be called after the stream has closed without generating an IOException.
public class CharArrayWriter
extends Writer

Constructors :
Constructors Description
CharArrayWriter() Creates a new CharArrayWriter.
CharArrayWriter(int initialSize) Creates a new CharArrayWriter with the specified initial size.
Important Methods :
Method Description
void write(int c) Writes a single character.
void write(char[] cbuf) Writes an array of characters.
void write(String str) Writes a string.
CharArrayWriter append(char c) Appends the specified character to this writer.
CharArrayWriter append(CharSequence csq) Appends the specified character sequence to this writer.
abstract void close() Closes the stream, flushing it first.
abstract void flush() Flushes the stream.
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.
Rest of Methods :
Method Description
void write(String str, int off, int len) Wites a portion of a string.
abstract void write(char[] cbuf, int off, int len) Writes a portion of an array of characters.
CharArrayWriter append(CharSequence csq, int start, int end) Appends a subsequence of the specified character sequence to this writer.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App