Java - CharArrayWriter Class



Introduction

The Java 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 

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
1

CharArrayWriter()

This creates a CharArrayReader from the specified array of chars.

2

CharArrayWriter(int initialSize)

This creates a new CharArrayWriter with the specified initial size.

Class methods

Sr.No. Method & Description
1 CharArrayWriter append(char c)

This method appends the specified character to this writer.

2 CharArrayWriter append(CharSequence csq)

This method appends the specified character sequence to this writer.

3 CharArrayWriter append(CharSequence csq, int start, int end)

This method appends a subsequence of the specified character sequence to this writer.

4 void close()

This method close the stream.

5 void flush()

This method flush the stream.

6 void reset()

This method resets the buffer so that you can use it again without throwing away the already allocated buffer.

7

int size()

This method returns the current size of the buffer.

8 char[] toCharArray()

This method returns a copy of the input data.

9 String toString()

This method converts input data to a string.

10 void write(char[] c, int off, int len)

This method writes characters to the buffer.

11 void write(int c)

This method writes a character to the buffer.

12 void write(String str, int off, int len)

This method write a portion of a string to the buffer.

13 void 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

Example - Appending a Single Character to a CharArrayWriter

The following example shows the usage of Java CharArrayWriter append(char c) method.

CharArrayWriterDemo.java

 package com.tutorialspoint; import java.io.CharArrayWriter; public class CharArrayWriterDemo { public static void main(String[] args) { // Creating a CharArrayWriter instance CharArrayWriter charArrayWriter = new CharArrayWriter(); // Appending characters one by one charArrayWriter.append('H'); charArrayWriter.append('i'); charArrayWriter.append('!'); // Converting the CharArrayWriter content to a string and displaying it System.out.println("CharArrayWriter content: " + charArrayWriter.toString()); // Closing the CharArrayWriter (not necessary, but good practice) charArrayWriter.close(); } } 

Output

Let us compile and run the above program, this will produce the following result −

 CharArrayWriter content: Hi! 

Explanation

  • A CharArrayWriter object is created.

  • The append(char c) method is used to append individual characters: 'H', 'i', and '!'.

  • The toString() method is called to retrieve and print the content stored in the CharArrayWriter.

Example - Appending a Subsequence from a StringBuilder

The following example shows the usage of Java CharArrayWriter append(CharSequence csq, int start, int end) method.

CharArrayWriterDemo.java

 package com.tutorialspoint; import java.io.CharArrayWriter; public class CharArrayWriterDemo { public static void main(String[] args) { CharArrayWriter writer = new CharArrayWriter(); // Create a StringBuilder StringBuilder sb = new StringBuilder("Spring Boot Framework"); // Append a substring from index 7 to 11 ("Boot") writer.append(sb, 7, 11); // Append another substring from index 12 to 21 ("Framework") writer.append(sb, 12, 21); // Print the output System.out.println(writer.toString()); // Output: BootFramework } } 

Output

Let us compile and run the above program, this will produce the following result −

 BootFramework 

Explanation

  • A StringBuilder "Spring Boot Framework" is created.

  • The append method extracts "Boot" (index 7 to 11) and appends it.

  • Another call extracts "Framework" (index 12 to 21) and appends it.

  • The final output is "BootFramework".

Example - Closing a CharArrayWriter and Writing Data After Closing

The following example shows the usage of Java CharArrayWriter close() method.

CharArrayWriterDemo.java

 package com.tutorialspoint; import java.io.CharArrayWriter; import java.io.IOException; public class CharArrayWriterDemo { public static void main(String[] args) throws IOException { CharArrayWriter writer = new CharArrayWriter(); // Write data to CharArrayWriter writer.write("Hello, Java!"); // Close the writer writer.close(); // Append more data after closing (allowed) writer.append(" Still working!"); // Convert to string and print System.out.println(writer.toString()); // Output: Hello, Java! Still working! } } 

Output

Let us compile and run the above program, this will produce the following result −

 Hello, Java! Still working! 

Explanation

  • A CharArrayWriter object is created.

  • "Hello, Java!" is written to it.

  • close() is called, but it does not affect further operations.

  • " Still working!" is appended even after closing.

  • The final output is "Hello, Java! Still working!".

Advertisements