Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java CharArrayWriter Class

In this tutorial, we will learn about CharArrayWrite class in Java. This class creates a buffer that is used to write data. This buffer has no fixed size and it grows automatically when data is written to the stream. There are other methods like toCharArray() and toString() to retrieve the data from the buffer.

Syntax

This is the syntax of CharArrayWriter class.

public class CharArrayWriter extends Writer  

Java CharArrayWriter class Methods

Methods of CharArrayWriter class are given in the following table.

Method Description
int size() This method is used to return the current size of the buffer.
char[] toCharArray() This method is used to return the copy of input data.
String toString() This method is used for converting input data to a string.
CharArrayWriter append(char c) This method is used to append the specified character to the writer.
CharArrayWriter append(CharSequence csq) This method is used to append the specified character sequence to the writer.
CharArrayWriter append(CharSequence csq, int start, int end) This method is used to append the subsequence of a specified character to the writer.
void write(int c) This method is used to write a character to the buffer.
void write(char[] c, int off, int len) This method is used to write a character to the buffer.
void write(String str, int off, int len) This method is used to write a portion of the string to the buffer.
void writeTo(Writer out) This method is used to write the content of the buffer to the different character streams.
void flush() This method is used to flush the stream.
void reset() This method is used to reset the buffer.
void close() This method is used to close the stream.

Example of CharArrayWriter Class

In the following example, we are implementing the write operation using CharArrayWriter class operation. Here we firstly created an object of CharArrayWriter class. Then we create an object of FileWriter class and using the writeTo() method we write data to the file.

package studytonight;
import java.io.CharArrayWriter;
import java.io.FileWriter;
public class StudyTonight 
{
	public static void main(String args[]) throws Exception
	{
		CharArrayWriter writer=new CharArrayWriter();    
		writer.write("Hello Studytonight");    
		FileWriter fileWriter=new FileWriter("E:\studytonight\\output.txt");   
		writer.writeTo(fileWriter);    
		fileWriter.close();    
		System.out.println("Data is written to the file successfully...");    
	}
}


Data is written to the file successfully...

output.txt

Hello Studytonight

Example of CharArrayWriter Class

In this example, we are implementing the public void write(String str, int offset, int length) method. Here str is the string which we want to write, offset is the start index in the string and length is the number of characters to be written from the string. We can see in the below program we are extracting the string from CharArrayWriter using the toString() method.

package studytonight;
import java.io.CharArrayWriter;
public class StudyTonight 
{
	public static void main(String args[]) throws Exception
	{
		CharArrayWriter writer = new CharArrayWriter(); 
		String str = "Hello Studytonight"; 
		writer.write(str, 6, 12);
		System.out.println(writer.toString()); 
	}
}


Studytonight

Conclusion:

In this tutorial, we learned about CharArrayWriter class, This class can be used to write common data to multiple files. This class creates a buffer that is used to write data. This buffer has no fixed size and it grows automatically when data is written to the stream.