Signup/Sign In

CharArrayWriter size() Method in Java

In this tutorial, we will learn about the size() method of CharaArrayWriter class in Java. This method is helpful to get the size of the current buffer for the CharArrayWriter.

Syntax

This is the syntax declaration of size() method this method does not accept any parameter and returns the size of the current buffer.

public int size()

Example of size() method of ChaArrayWriterClass

In the example given below, we are writing two characters to the object of a CharArrayWriter class so its size becomes 2, when we call the size() method on CharArrayWriter class this method will return 2 as the size of the buffer.

import java.io.CharArrayWriter;
import java.io.IOException;
public class StudyTonight 
{
	public static void main(String[] args) throws IOException 
	    { 
	        CharArrayWriter arr = new CharArrayWriter(); 
	        arr.write(65);
	        arr.write(66);
	        //calculates the current size of buffer
	        int size = arr.size();
	        System.out.println("Current size of buffer: "+size);
	  
	    } 
}


Current size of buffer: 2

Example of size() Method

In this example, instead of writing, we are appending the data to the CharArrayWriter and then we are checking the buffer size and still it will give the correct output. This shows that it does not depend on the method of writing data to the CharSequence but the output depends on the size of the buffer.

import java.io.CharArrayWriter;
import java.io.IOException;
public class StudyTonight 
{
	public static void main(String[] args) throws IOException
	{
		CharArrayWriter charArrayWriter= new CharArrayWriter();

		CharSequence sequence1 = "study";
		CharSequence sequence2 = "tonight";

		charArrayWriter.append(sequence1);
		charArrayWriter.append(sequence2);
		
		System.out.println("String: "	+ charArrayWriter.toString());
		System.out.println("Size: "	+ charArrayWriter.size());
	}   
}


String: studytonight
Size: 12

Conclusion:

In this tutorial, we learned about the size() method of CharArrayWriter classs. This method is helpful to get the size of the current buffer for the CharArrayWriter.



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.