Java - CharArrayWriter size() method



Description

The Java CharArrayWriter size() method is used to return the number of characters currently stored in the buffer.

Declaration

Following is the declaration for java.io.CharArrayWriter.size() method −

public int size()

Parameters

NA

Return Value

This method returns an int representing the current size of the buffer.

Exception

NA

Example - Checking Size After Writing Characters

The following example shows the usage of Java CharArrayWriter size() method. This example shows how the size() method reflects the number of characters stored after writing data.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;
import java.io.IOException;

public class CharArrayWriterDemo {
   public static void main(String[] args) {
      try {
         // Creating CharArrayWriter instance
         CharArrayWriter writer = new CharArrayWriter();

         // Writing data to writer
         writer.write("Hello");
         System.out.println("Size after writing 'Hello': " + writer.size());

         // Writing more data
         writer.write(", World!");
         System.out.println("Size after writing ', World!': " + writer.size());

         // Closing the writer (optional)
         writer.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Size after writing 'Hello': 5
Size after writing ', World!': 13

Explanation

  • We create a CharArrayWriter instance.

  • We write "Hello", and size() returns 5.

  • We append ", World!", and size() reflects the total count (13).

Example - Checking Size After Resetting the Writer

The following example shows the usage of Java CharArrayWriter size() method. This example shows how size() returns 0 after calling reset().

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;
import java.io.IOException;

public class CharArrayWriterDemo {
   public static void main(String[] args) {
      try {
         // Creating CharArrayWriter instance
         CharArrayWriter writer = new CharArrayWriter();

         // Writing some data
         writer.write("Java Programming");
         System.out.println("Size before reset: " + writer.size());

         // Resetting the writer
         writer.reset();
         System.out.println("Size after reset: " + writer.size());

         // Closing the writer (optional)
         writer.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Size before reset: 16
Size after reset: 0

Explanation

  • We write "Java Programming", and size() returns 17.

  • We call reset(), which clears the buffer.

  • size() returns 0, confirming the buffer is empty.

Example - Checking Size in a Loop While Appending Data

The following example shows the usage of Java CharArrayWriter size() method. This example dynamically writes characters and checks size() in a loop.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {
        
      // Creating CharArrayWriter instance
      CharArrayWriter writer = new CharArrayWriter();

      // Writing data character by character
      String text = "Size Test";
      for (int i = 0; i < text.length(); i++) {
         writer.write(text.charAt(i));
         System.out.println("Size after writing '" + text.charAt(i) + "': " + writer.size());
      }

      // Closing the writer (optional)
      writer.close();
   }
}

Output

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

Size after writing 'S': 1
Size after writing 'i': 2
Size after writing 'z': 3
Size after writing 'e': 4
Size after writing ' ': 5
Size after writing 'T': 6
Size after writing 'e': 7
Size after writing 's': 8
Size after writing 't': 9

Explanation

  • We write "Size Test" character by character.

  • After each character, we check size() to observe how it increases.

java_io_chararraywriter.htm
Advertisements