Sunday, February 22, 2026
HomeLanguagesJavaCharArrayWriter reset() method in Java with examples

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()
 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS