PUBLISHED ON: MARCH 1, 2021
CharArrayWriter reset() Method in Java
In this tutorial, we will learn about the reset()
method of CharArrayWriter
class in Java
.The reset()
method of CharArrayWriter
class is used to reset the stream so that you can use it again without throwing away the already allocated stream.
Syntax
This is the syntax of the reset method, this method does not take any parameter and does not return any value.
public void reset();
Example 1
Here is the example of the reset() method of CharaArrayWriter class, firstly we append the string and print it, and then we again try to write the data but before that, we called the reset() method to reset the allocation of the buffer.
import java.io.CharArrayWriter;
import java.io.IOException;
class StudyTonight
{
public static void main(String[] args) throws IOException
{
CharArrayWriter chw = new CharArrayWriter();
CharSequence csq = "Hello Studytonight";
chw.append(csq);
System.out.println(csq);
chw.reset();
csq = "Hello World";
chw.append(csq);
System.out.println(chw.toString());
}
}
Hello Studytonight
Hello World
Example 2
Here we can see the difference between the size of CharArrayWriter
after calling the reset()
method and before calling the reset()
method. This method is used to reset the buffer so that it can be used again without throwing away the already allocated buffer, that's why we are getting the size of the buffer of CharArrayWriter is 5 and after reset() method call it is 0 because all the allocated buffer is wiped out.
import java.io.CharArrayWriter;
import java.io.IOException;
class StudyTonight
{
public static void main(String[] args) throws IOException
{
CharArrayWriter charArrayWriter= new CharArrayWriter();
for (int c = 65; c < 70; c++)
{
charArrayWriter.write(c);
}
System.out.println("Size of charArrayWriter: "+ charArrayWriter.size());
charArrayWrite.reset();
System.out.println("Size of charArrayWriter: "+ charArrayWriter.size());
}
}
Size of charArrayWriter: 5
Size of charArrayWriter: 0
Conclusion:
In this tutorial, we learned about the reset() method from CharArrayWriter
class in Java. The reset()
method of CharArrayWriter
class is used to reset the stream so that you can use it again without throwing away the already allocated stream.