Signup/Sign In

CharArrayWriter write() Method in Java

In this tutorial, we will learn about write() method of CharArrayWriter class in Java. The write() method is used to write the characters to the buffer. The buffer automatically grows when data is written to the stream. The data can be retrieved using toCharArray() and toString() method.

Syntax

This method is used to write a character to the writer in form of an integer. This write() method writes one character at a time to the CharArrayWriter.

public void write(int c)

This method is used to write a character to the writer in form of a character array. It is an overloaded method.

public void write(char[] c, int offset, int length)

This method is used to write a character to the writer in form of a string array. It is an overloaded method.

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

Example 1: Writer Characters In Java

In this example, we are passing a single character to the write() method of CharArrayWriter to check what is written we have a method toString() and it will tell us the data of CharacterArrayWriter

import java.io.CharArrayWriter;
public class StudyTonight 
{
	public static void main(String[] args) 
	{ 
		CharArrayWriter charArrayWriter = new CharArrayWriter(); 
		charArrayWriter.write(65); 
		System.out.println(charArrayWriter.toString()); 
	} 
}


A

Example 2: Writer Characters In Java

In this example, we are passing an array of characters to the write() method and it will write it to the CharArrayWriter we can specify the range of the characters from the array to be written after passing the start index and end index of the character.

import java.io.CharArrayWriter;
public class StudyTonight 
{
	public static void main(String[] args) 
	{ 
		CharArrayWriter charArrayWriter = new CharArrayWriter(); 
        char[] arr = { 'H', 'E', 'L', 'L', 'O', 'S', 'T', 'U', 'D', 'Y', 'T', 'O', 'N', 'I', 'G', 'H', 'T'}; 
        charArrayWriter.write(arr, 0, 5); 
		System.out.println(charArrayWriter.toString()); 
	} 
}


HELLO

Example 3: Writer Characters In Java

In this program, we are passing the string to the write() method and we also pass the start index and end index to ensure that it will write data from this specific range only.

import java.io.CharArrayWriter;
public class StudyTonight 
{
	public static void main(String[] args) 
	{ 
		CharArrayWriter charArrayWriter = new CharArrayWriter(); 
        String str = "Hello Studytonight"; 
        charArrayWriter.write(str, 8, 5); 
		System.out.println(charArrayWriter.toString()); 
	} 
}


Hello

Conclusion

In this tutorial, we learned about the write() method of CharArrayWrite class in Java. The write() method of Java CharArrayWriter is used to write the characters to the buffer.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.