CharArrayWriter reset() method in Java with examples

The reset() method of the CharArrayWriter class in Java is used to reset the buffer so that it can be used again without throwing away the already allocated buffer.
Syntax:Â
Â
public void reset()
Parameters: This method does not accept any parameter.
Return Value: This method does not returns anything.Â
Below program illustrate the above method:Â
Program 1:Â
Â
Java
// Java program to illustrate// the reset() methodÂ
import java.io.*;Â
public class GFG {    public static void main(String[] args)        throws IOException    {Â
        // Initializing the character array        char[] geek = { 'G', 'E', 'E', 'K', 'S' };Â
        // Initializing the CharArrayWriter        CharArrayWriter char_array1            = new CharArrayWriter();Â
        for (int c = 72; c < 77; c++) {            // Use of write(int char)            // Writer int value to the Writer            char_array1.write(c);        }Â
        // Use of size() method        System.out.println("\nSize of char_array1 : "                           + char_array1.size());Â
        // Resets the current stream        char_array1.reset();Â
        // Use of size() method        System.out.println("Size of char_array1 : "                           + char_array1.size());    }} |
Output:Â
Size of char_array1 : 5 Size of char_array1 : 0
Â
Program 2:Â
Â
Java
// Java program to illustrate// the reset() methodÂ
import java.io.*;Â
public class GFG {    public static void main(String[] args)        throws IOException    {Â
        // Initializing the character array        char[] geek            = { 'G', 'O', 'P', 'A', 'L', 'L' };Â
        // Initializing the CharArrayWriter        CharArrayWriter char_array1            = new CharArrayWriter();Â
        for (int c = 72; c < 78; c++) {            // Use of write(int char)            // Writer int value to the Writer            char_array1.write(c);        }Â
        // Use of size() method        System.out.println("\nSize of char_array1 : "                           + char_array1.size());Â
        // Resets the current stream        char_array1.reset();Â
        // Use of size() method        System.out.println("Size of char_array1 : "                           + char_array1.size());    }} |
Output:Â
Size of char_array1 : 6 Size of char_array1 : 0
Â
Reference: https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#reset()
Â



