CharArrayWriter 

Introduction

The Java.io.CharArrayWriter class implements a character buffer that can be used as an Writer. The buffer automatically grows when data is written to the stream.

Class declaration

Following is the declaration for Java.io.CharArrayWriter class −

public class CharArrayWriter
   extends Writer

Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

Field

Following are the fields for Java.io.CharArrayWriter class −

  • protected char[] buf − This is the buffer where data is stored.
  • protected int count − This is the number of chars in the buffer.
  • protected Object lock − This is the object used to synchronize operations on this stream.

Class constructors

Sr.No.Constructor & Description
1CharArrayWriter()This creates a CharArrayReader from the specified array of chars.
2CharArrayWriter(int initialSize)This creates a new CharArrayWriter with the specified initial size.

Class methods

Sr.No.Method & Description
1CharArrayWriter append(char c)This method appends the specified character to this writer.
2CharArrayWriter append(CharSequence csq)This method appends the specified character sequence to this writer.
3CharArrayWriter append(CharSequence csq, int start, int end)This method appends a subsequence of the specified character sequence to this writer.
4void close()This method close the stream.
5void flush()This method flush the stream.
6void reset()This method resets the buffer so that you can use it again without throwing away the already allocated buffer.
7int size()This method returns the current size of the buffer.
8char[] toCharArray()This method returns a copy of the input data.
9String toString()This method converts input data to a string.
10void write(char[] c, int off, int len)This method writes characters to the buffer.
11void write(int c)This method writes a character to the buffer.
12void write(String str, int off, int len)This method write a portion of a string to the buffer.
13void writeTo(Writer out)This method writes the contents of the buffer to another character stream.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.Writer
  • Java.io.Object

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *