ByteArrayOutputStream reset() method in Java with Examples
The reset() method of ByteArrayOutputStream class in Java is used to reset the ByteArrayOutputStream and make the count field of this ByteArrayOutputStream to zero. As a result of this all currently accumulated output in this ByteArrayOutputStream is discarded. This ByteArrayOutputStream can be used again by reusing the already allocated buffer space.
Syntax:
public void reset()
Parameters: This method does not accept any parameter.
Return value: This method does not return any value.
Exceptions: This method does not throw any exception.
Below programs illustrate reset() method in ByteArrayOutputStream class in IO package:
Program 1:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws Exception
{
ByteArrayOutputStream byteArrayOutStr
= new ByteArrayOutputStream();
byte [] buf1 = { 65 , 66 , 67 , 68 , 69 };
byte [] buf2 = { 71 , 69 , 69 , 75 , 83 };
byteArrayOutStr.write(buf1);
System.out.println(
byteArrayOutStr.toString());
byteArrayOutStr.reset();
byteArrayOutStr.write(buf2);
System.out.println(
byteArrayOutStr.toString());
}
}
|
Program 2:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws Exception
{
ByteArrayOutputStream byteArrayOutStr
= new ByteArrayOutputStream();
byte [] buf1 = { 71 , 69 , 69 , 75 , 83 };
byte [] buf2 = { 70 , 79 , 82 };
byteArrayOutStr.write(buf1);
System.out.println(
byteArrayOutStr.toString());
byteArrayOutStr.reset();
byteArrayOutStr.write(buf2);
System.out.println(
byteArrayOutStr.toString());
byteArrayOutStr.reset();
byteArrayOutStr.write(buf1);
System.out.println(
byteArrayOutStr.toString());
}
}
|
References:
https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayOutputStream.html#reset()
Last Updated :
28 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...