Java - CharArrayWriter reset() method



Description

The Java CharArrayWriter reset() method is used to clear the current contents of the writer and reset the buffer, allowing it to be reused without creating a new instance.

Declaration

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

public void reset()

Parameters

NA

Return Value

This method does not return any value.

Exception

NA

Example - Using CharArrayWriter reset() method

The following example shows the usage of Java CharArrayWriter reset() method.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {      
      CharArrayWriter chw = null;
      
      try {
         // create character array writer
         chw = new CharArrayWriter();
         
         // declare character sequence
         CharSequence csq = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
         
         // append character sequence to the writer
         chw.append(csq);
         
         System.out.println("Before Reset:");
         
         // print  character sequence
         System.out.println(csq);
         
         // invoke reset()
         chw.reset();
         System.out.println("Reset is invoked");
         
         csq = "1234567890";
         
         chw.append(csq);
         
         System.out.println("After reset:");
         
         // print character sequence
         System.out.println(chw.toString());
               
      } catch(Exception e) {
         // for any error
         e.printStackTrace();
      } finally {
         // releases all system resources from writer
         if(chw!=null)
            chw.close();
      }
   }
}

Output

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

Before Reset:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Reset is invoked
After reset:
1234567890

Example - Using reset() to Clear the Buffer

The following example shows the usage of Java CharArrayWriter reset() method. In this example, we write some data to a CharArrayWriter, display its contents, then reset it and write new 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, World!");
         System.out.println("Before reset: " + writer.toString());

         // Resetting the writer
         writer.reset();

         // Writing new data after reset
         writer.write("New Data");
         System.out.println("After reset: " + writer.toString());

         // 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 −

Before reset: Hello, World!
After reset: New Data

Explanation

  • A CharArrayWriter is created.

  • The string "Hello, World!" is written into the buffer.

  • The buffer is printed to confirm it holds the expected data.

  • The reset() method is called, clearing the buffer.

  • A new string "New Data" is written to the buffer.

  • The buffer is printed again, showing only the new data.

Example - Using reset() in a Loop for Reuse

The following example shows the usage of Java CharArrayWriter reset() method. In this example, we demonstrate reusing the same CharArrayWriter multiple times.

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 and resetting multiple times
         for (int i = 1; i <= 3; i++) {
            writer.write("Iteration " + i);
            System.out.println("Iteration " + i + " Output: " + writer.toString());

            // Reset buffer after each iteration   
            writer.reset();
         }

         // 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 −

Iteration 1 Output: Iteration 1
Iteration 2 Output: Iteration 2
Iteration 3 Output: Iteration 3

Explanation

  • A CharArrayWriter is created once.

  • Inside a loop, "Iteration X" is written to the buffer and printed.

  • The reset() method is called to clear the buffer for the next iteration.

  • This allows the same CharArrayWriter to be reused multiple times without creating a new instance.

java_io_chararraywriter.htm
Advertisements