Open In App

Java CharArrayWriter Class | Set 1

Last Updated : 18 Apr, 2025
Comments
Improve
Suggest changes
15 Likes
Like
Report

The CharArrayWriter class in Java is part of the java.io package, and it is used to write data into a character array. This class is used when we want to store characters temporarily without dealing with files and other storage.

Features of CharArrayWriter Class:,

  • It is used to store characters in memory, not in files.
  • The internal buffer increases automatically as more characters are written to it.
  • We can also convert the stored characters into a String[] or a char[] array.
  • It is useful when we want to store the characters temporarily in memory.

Example:


Output
Geeks


Declaration of CharArrayWriter Class

The Declaration of CharArrayWriter class is listed below:

public class CharArrayWriter extends Writer

All Implemented Interfaces:

  • Closeable: This interface make sure that all the resources are properly released when we are done using the CharArrayWriter.
  • Flushable: This inteface flush the content and making sure any stored data is written out.
  • Appendable: This interface lets us add characters to the writer.
  • AutoCloseable: This interface enables automatic resource management.

Constructors in CharArrayWriter in Java

This class consists of two constructors with the help of which we can create object of this class in different ways. The following are the constructors available in this class:

1. CharArrayWriter(): This constructor creates a new CharArrayWriter with an initial buffer size of 32 characters.

Syntax:

CharArrayWriter charArrayWriter = new CharArrayWriter();

Example:


Output
One


2. CharArrayWriter(int size): This constructor creates a new CharArrayWriter with the specified initial buffer size.

Syntax:

CharArrayWriter charArrayWriter = new CharArrayWriter(64);

Example:


Output
Java


Java CharArrayWriter Methods

The image below demonstrates the methods of CharArrayWriter class.

CharArrayWriter class in Java - Set 1


Now, we are going to discuss about each method one by one in detail:

1. write(int char): This method is used to writes a single character to the writer.

Syntax:

public void write(int char)

Parameters: The integer value of the character to be written.


2. write(String str, int offset, int maxlen): This method is used to writes a part of a string to the writer.

Syntax:

public void write(String str, int offset, int maxlen)

Parameters: This method includes three parameters which are listed below

  • str: The string to be written to the Writer.
  • offset: It is the starting position of the string.
  • maxlen: Maximum number of characters to write from the string.


3. write(char[] carray, int offset, int maxlen): This method is used to writes a part of a character array to the Writer. We can tell where to start in the array and how many characters to write.

Syntax:

public void write(char[] carray, int offset, int maxlen)

Parameters: This method includes three parameters which are listed below

  • carray: The character array to be written to the Writer.
  • offset: It is the starting position in the character array
  • maxlen: Maximum number of characters to write from the character array.


4. writeTo(Writer out_stream): This method is used to Writes the content of the buffer to another specified stream.

Syntax:

public void writeTo(Writer out_stream)

Parameters: The destination Writer stream to which the buffer content will be written.


5. toString(): This method is used to convert the buffer content into a string representation.

Syntax:

public String toString()

  • Parameters: This method does not take any parameter
  • Return Type: This method returns the buffer content as a string


6. close(): This method closes the writer stream but does not release the buffer.

  • Parameters: This method does not take any parameter.


7. size(): This method returns the size of a buffer.

  • Parameters: This method does not take any parameter.


Java Program to Demonstrate Methods of CharArrayWriter Class

Example:


Output:

Using write(int char) : HIJKL
write(str, offset, maxlen) : JKL
write(carray, offset, maxlen) : EKS
char_array3 to char_array1 : HIJKLEKS
Size of char_array1 : 8
Size of char_array1 : 3
Size of char_array1 : 3


To know about other methods, refer this article: Java.io.CharArrayWriter class in Java | Set2


Next Article
Article Tags :
Practice Tags :

Similar Reads